2016-02-13 46 views
1

我很難理解爲什麼這不起作用。我可以訪問父類的屬性,但不能訪問對象。瞭解爲什麼父級屬性可以從子類訪問,但父對象不能

我讀過其他線程,表明我應該從下面的Session類中調用parent::__construct(),但這只是創建一個無限循環並失敗。

爲什麼$TestVariable在Session類中可用,但$Cookie對象不是?

主應用程序文件:

class Application 
{ 
    //Class Objects 
    public $Cookie = NULL; 
    public $Session = NULL; 
    public $TestVariable = "Hello World"; 

    //Object initialization. Singleton design. 
    protected function __construct() 
    { 
     //Initialize cookie object. 
     $this->Cookie = Cookie::getCookie(); 

     //Initialize session object. 
     $this->Session = Session::getSession(); 
    } 

    //Returns the singleton instance of The Curator class. Singleton design. 
    public static function Initialize() 
    { 
     static $instance = NULL; 

     if($instance === NULL) 
     { 
      $instance = new static(); 
     } 

     return $instance; 
    } 

    //Singleton design. 
    private function __clone() {} 

    //Singleton design. 
    private function __wakeup() {} 
} 

Cookie文件:

class Cookie 
{ 
    //Object initalization. Singleton design. 
    protected function __construct() 
    { 
     //CODE HERE 
    } 

    //Singleton design. 
    private function __clone() {} 

    //Singleton design. 
    private function __wakeup() {} 

    //Returns the singleton instance of the cookie class. Singleton design. 
    public static function getCookie() 
    { 
     static $cookieInstance = NULL; 

     if($cookieInstance === NULL) 
     { 
      $cookieInstance = new static(); 
     } 

     return $cookieInstance; 
    } 

    //Removes all cookies. 
    public function destroyCookies() 
    { 
     //CODE HERE 
    } 
} 

會議文件:

class Session extends Application 
{ 
    //Object initialization. Singleton design. 
    protected function __construct() 
    { 
     //This FAILS! 
     $this->Cookie->destroyCookies(); 

     //This WORKS! 
     echo $this->TestVariable; 
    } 

    //Singleton design. 
    private function __clone() {} 

    //Singleton design. 
    private function __wakeup() {} 

    //Returns the singleton instance of the session class. Singleton design. 
    public static function getSession() 
    { 
     static $sessionInstance = NULL; 

     if($sessionInstance === NULL) 
     { 
      $sessionInstance = new static(); 
     } 

     return $sessionInstance; 
    } 
} 
+0

可能是因爲您的會話(雖然它可能會擴展應用程序類)正在實例化爲您創建的每個應用程序實例中的新實例.....要麼使用擴展名,要麼使用組合;但不要玩挑選混合;至少,這是令人困惑的 –

回答

0

你需要調用從子類的父類的構造。 而遞歸是因爲你有這樣的:

  • 會話構造函數調用父:: __結構()
  • 應用構造函數調用會話::的getSession()
  • 的會話中::的getSession(),我們有new Session聲明
  • 會話構造函數被調用
  • 會話構造函數調用父:: __結構()
  • ...等

因此,使它工作,你需要打破鏈和移動會話初始化了應用程序構造:

class Application 
{ 
    //Class Objects 
    public $Cookie = NULL; 
    public $Session = NULL; 
    public $TestVariable = "Hello World"; 

    //Object initialization. Singleton design. 
    protected function __construct() 
    { 
     //Initialize cookie object. 
     $this->Cookie = Cookie::getCookie(); 
    } 

    protected function init() { 
     //Initialize session object. 
     $this->Session = Session::getSession(); 
    } 

    //Returns the singleton instance of The Curator class. Singleton design. 
    public static function Initialize() 
    { 
     static $instance = NULL; 

     if($instance === NULL) 
     { 
      $instance = new static(); 
      // Call init after construction 
      $instance->init(); 
     } 

     return $instance; 
    } 

... 

這裏是full code

但的確,設計看起來不對,可能Session根本不應該繼承Application