singleton - trying to initialise a static property on creation fails

后端 未结 2 1538
一向
一向 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

    <?
    
    class ErrorHandling{
    
      private static $instance;
      private static $_appMode;  // not initialised in returned instance
      private $_errorConfig;
    
     private function __construct(array $config, $appMode = null){
    
       $this->_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

    0 讨论(0)
  • 2021-01-26 13:48

    You probably don't use any 5.3.x-Version of PHP. "Late Static Binding" (static::$_appMode) is not available in any version before. Use self::$_appMode instead. Its slightly different from static, but in your case it should be OK. For further info read Manual: Late Static Binding

    0 讨论(0)
提交回复
热议问题