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