singleton - trying to initialise a static property on creation fails

后端 未结 2 1541
一向
一向 2021-01-26 13:15

I have a singleton class used to initialise error_handling.

The class as is takes a Zend_Config object and optional $appMode in parameter, to allow overriding the def

2条回答
  •  天涯浪人
    2021-01-26 13:37

    Checkout

    _errorConfig = $config;
    
       if(isset($appMode)){
           self::$_appMode = $appMode;
       }else{
             self::$_appMode = APPMODE;
       }  
     }
    
     private final function  __clone(){}
    
     public static function getInstance(array $config, $appMode = null){
         if(! (self::$instance instanceof self)){
             self::$instance = new ErrorHandling($config, $appMode);
         } 
         return self::$instance;
     }
    
     public static function getAppMode() {
        return self::$_appMode;
     }
    } 
    
    $e = ErrorHandling::getInstance(array('dev' => true), -255);
    var_dump($e, ErrorHandling::getAppMode());
    

    Is that your want?

    Your could read here about difference between static and self- late static binding

提交回复
热议问题