多态性 - 确保一切正确[关闭]

I have a class that is responsible for handling all configurations - reading configs from files, and getting basic configuration variables that were set in index.php (+ setting them from there).

Thus, I decided to use polymorphism here - I made Config class abstract and extended this with FILE and VARIABLE classes.

Is it a good practice behaviour, if the base class with these two responsibilities is like 100 lines long?

Don't downvote me here - I just don't want to find out that it's not a flexible solution when the project is already done.

This is the code (without refactoring, testing, and adding a couple of functions though, but the concept should be clear).

class Config {

    private $file;

    public static $configs = array();

    /**
     * Initializes basic website configurations such as base URL, or the name
     * of the index file.
     *
     * These values can be accessed through this class
    */
    public static function init($configs = array())
    {
        foreach($configs as $key => $value)
        {
            self::$configs[$key] = $value;
        }
    }

    /**
     * Returns the configuration variable which is set in the index file
     *
     * @param string $attribute
     * @return multitype:
     */
    public function __get($attribute)
    {
        return ($this->configs[$attribute]) ? $this->configs[$attribute] : -1;
    }

    /**
     * Setting path to the config file.
     * 
     * @param string $module
     */
    private function __construct($module)
    {
        // Path to the config file
        $path = APATH . 'config' . DIRECTORY_SEPARATOR . $module . '.php';

        // Set the config file to the attribute here
        $this->file = include $path;
    }

    /**
     * Return the object.
     *
     */
    public static function factory($module)
    {
        return new Config($module);
    }

    /**
     * Loads configurations from the given file.
     *
     */
    public function load($property)
    {
        // Return requested value
        return $array[$property];
    }

}

There's nothing wrong with what you're doing but it makes me wonder why you want to do it this way.

If you're trying to enforce the handling of config variables in a particular way then maybe load them once in a static class. If you're trying to practice abstraction then it doesn't really matter if it's 100 lines or 1K or whatever long.

It does make me wonder why you'd have config variables scattered among lots of different files such that it would require encapsulating the loading process like this. Typically config information is loaded once at startup and kept. What happens if a file / class somewhere 'down the road' after your app starts doesn't load the config or just ignores your implementation?

If nothing else, you'll probably want to make 'init' private and call it from your constructor. Otherwise one could call 'factory' but ignore this step and assume that no config information existed. Also - if 'configs' is static then '$this->configs' seems a bit sketchy.