mercredi 24 août 2016

Is Making a Singleton Configuration File Correct?

I have created a configuration file which only contains basic config such as paths and url. To avoid multiple instance when declaring the class I made a singleton to make sure that the config file class has only one instance for reusability so I can use it on another class. Is it correct?

class Configuration
{
    private static $instance;
    private $configurations = array( "root_path"         => "C:/xampp/htdocs/essentials",
                                     "root_url"          => "http://ift.tt/2bgYVnj",
                                     "global_class_path" => "/resources/scripts/php/global/classes/",
                                     "login_class_path"  => "/resources/scripts/php/login/classes/",
                                     "time_zone"         => "Asia/Manila" );

    /* Get the instance of the class */
    public static function getInstance()
    {
        if ( empty( static::$instance ) ) {
            static::$instance = new static();
        }

        return static::$instance;
    }

    /* Prevent creating of new instance if already existed */
    private function __construct() {}

    /* Prevent cloning of instance */
    private function __clone() {}

    /* Prevent unserializing of instance */
    private function __wakeup() {}

    public function getConfiguration( $configuration_id ) {
        return ( ( empty( $this->configurations[ $configuration_id ] ) )
               ? null
               : $this->configurations[ $configuration_id ] );
    }
}




Aucun commentaire:

Enregistrer un commentaire