理解为什么可以从子类访问父属性但父对象不能

时间:2016-02-13 21:17:26

标签: php oop inheritance

我很难理解为什么这不起作用。我可以访问我父类的属性,但不能访问对象。

我已经读过其他线程,表明我应该从下面的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;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要从子类调用父构造函数。 递归是因为你有这个:

  • 会话构造函数调用parent :: __ construct()
  • 应用程序构造函数调用Session :: getSession()
  • 在Session :: getSession()中,我们有new Session语句
  • 调用会话构造函数
  • 会话构造函数调用parent :: __ construct()
  • ......等等

因此,要使其工作,您需要打破链并将会话初始化移出Application构造函数:

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