Stop using `global` in PHP

后端 未结 6 1943
醉梦人生
醉梦人生 2020-11-22 03:45

I have a config.php that is included to each page. In config I create an array that looks something like:

$config = array();
$config[\'site_name\         


        
6条回答
  •  逝去的感伤
    2020-11-22 04:24

    I created an easy small class:

    class Config {
    
        private static $config = array();
    
        public static function set( $key, $value ) {
            self::$config[$key] = $value;
        }
    
        public static function get( $key ) {
            return isset( self::$config[$key] ) ? self::$config[$key] : null;
        }
    }
    
    Config::set( 'my_config', 'the value' );
    
    echo 'the config value is: ' . Config::get('my_config');
    

    this can easly be refactored to have a function isSet( $key ) or maybe a setAll( $array ).

    EDIT: Now the syntax should be valid.

    you can easily modify this class like follows:

    class Config {
    
        private static $config = array();
    
        public static function set( $key, $value ) {
            self::$config[$key] = $value;
        }
    
        public static function get( $key ) {
            return isset( self::$config[$key] ) ? self::$config[$key] : null;
        }
    
        public static function setAll( array $array ) {
            self::$config = $array;
        }
    
        public static function isKeySet( $key ) {
            return isset( self::$config[ $key ] );
        }
    }
    
    Config::setAll( array(
        'key' => 'value',
        'key2' => array( 'value',
                        'can be an',
                        'array' ) ) );
    Config::set( 'my_config', 'the value' );
    
    if( Config::isKeySet( 'my_config' ) ) {
        echo 'the config value is: ' . Config::get('my_config');
    }
    

    You still need to include the file in any another file that uses configs, or use an autoloader.

    EDIT 2:

    It's pretty much the same as using a global, with the difference you don't need to state that you want to use it in the beginning of every function. If you want to use Configs globally, then the Configs have to be, in some way global. When putting something in the global scope, you need to argue if this can be dangerous information to an other class not meant to see this information... default configurations? I think it's safe to have in the global scope, and then you just need something that is easy to modify and customize.

    If you decide that it's dangerous information, that should not be reachable for a class other then the class it's meant for, then you might want to check in to Dependency injection. With dependency injections a class will take an object in it's constructor, placing it privately in a variable to use. This object can be an object from a configuration class, and then you need a wrapper class creating first the configuration object, and then the Template object injecting the configurations. This is a design often seen in more complex design patterns, like for instance Domain Driven Design.

提交回复
热议问题