通过项目传递数组

Everybody has it, an array or object that needs accessing throughout your entire project. This can be settings, an array of user data or anything else that does not qualify for for a defined variable (cause defined array's are bad m'kay).

I'm probably not looking for the right type of thing, but I need a way to access an object in every class, function or page.

So for example sake, lets say I have a standard config file with this:

<?php
return array(
    'database' => array(
        'driver' => 'mysql',
        'mysql' => array(
           'host' => 'localhost',
           ...
        ),
        'mongodb' => array(
            'host' => 'localhost',
            ...
        ),
        ...
    ),
);
?>

I get it by doing:

$config = (require('path/file.php'));

So, now I have my config. But this doesn't pass around. I don't want to call "$config = (require('path/file.php'));" everytime I need a setting.

So is there a way to always call this only once by, for example, storing it in a class or something (without having to define that class all the time obviously, cause that defeats the point).

Now I know about globals. But I'm not really partial to them for several reasons. Mysql is not an option cause it's a config file and users need to be able to alter it before launch.

Not tested

In Config.php

class Config
{
    public static function get()
    {
        return array(
            'database' => array(
                'driver' => 'mysql',
                'mysql' => array(
                    'host' => 'localhost',
                    ...
                ),
                'mongodb' => array(
                    'host' => 'localhost',
                    ...
                ),
                ...        
            ),
        );
    }
}

In your index.php:

function __autoload($classname) {
    $filename = "./". $classname .".php";
    include_once($filename);
}

In the file you want your conf:

$myConf = Config::get();

You can also improve the class to easily get a particular config data

<?php

class config {
    public static $driver = "mysql";
    public static $host = "localhost";
}

echo config::$driver;

If your goal is to return an entire array follow niconoes example.

If you don't want to have to make a object you can do it like the code posted above. Its quick and simple.

So in the end this is my solution which is loosly based niconoe, or at least derived from his answer.

Class1

Class Class1 {

    public static $conf = null;

    public function __construct($config) {
        self::$conf = $config;
    }

}

Class2

Class Class2 extends Class1 {

    public function conf() {
        return self::$conf;
    }

}

index

// Your custom class dir
define('CLASS_DIR', 'classes/');

// Add your class dir to include path
set_include_path(get_include_path().PATH_SEPARATOR.CLASS_DIR);

// Load all classes
spl_autoload_extensions('.class.php');

// Use default autoload implementation
spl_autoload_register();

// define the config (for now a string, but array is possible too
$cClass2 = new Class2('config');

// echo the variable in Class1 by requesting a Class2 function
echo Class2::conf();

Outputs "config"

This enables passing of variables between classes and loading all of them at once.

I'd love to hear some comments seeing I'm still learning and appreciate all of your opinions.