PHP与命名空间和单例有关

时间:2013-05-06 23:03:34

标签: php namespaces singleton

嘿伙计我有一个问题,我认为是因为命名空间,我有我的父类TheParent,我做了一些事情,把它添加到$this然后扩展到子类期望$this将继续,但除了在父母构造函数中明确提到的内容之外的所有内容似乎都消失了($timeout = 10)我试图弄清楚我在哪里开了这个代码,如果任何人都可以向我解释为什么这不像我认为的那样工作?

 Namespace Services;

 Class TheParent
 {
    public function __construct($timeout = 10, array $options = array())
    {
        $this->setAuth($options)->setTimeout($timeout);
    }

    // other methods that put information into $this

    public function useRest()
    {
        require_once 'RestAggregator.php'

        $this->message = REST::getInstance();

        header('Content-Type: text/plain');
        print_r($this); die;

    }
 }


 Namespace Services;

 Class REST Extends TheParent
 {
    private static  $instance   = NULL;
    private         $messages   = array();

    public function __construct()
    {
        $this->messages = self::getDataMessages();
    }

    public static function getInstance()
    {
        if(! isset(REST::$instance))
        {
            REST::$instance = New REST();
        }

        return REST::$instance;
    }

    protected function getDataMessages()
    {
        return REST::$instance->messages = array(
          'foo'   => '4',
          'bar'   => '5',
          'baz'   => '6',
        );
    }
 }

这是返回的其余对象,你会想到我也会有来自TheParent的数据,这些数据在传递给_appKey <之前已经定义了REST等等。 / p>

Services\REST Object
(
    [type] => 
    [messages:Services\REST:private] => Array
        (
        )

    [groups:Services\REST:private] => Array
        (
        )

    [_following:protected] => 
    [_sent:protected] => 
    [_private:protected] => 
    [_received:protected] => 
    [_appKey:protected] => 
    [_appSecret:protected] => 
    [_authToken:protected] => 
    [_authSecret:protected] => 
    [_authCode:protected] => 
    [_redirectUri:protected] => 
    [_smAuth:protected] => 
    [_accessToken:protected] => 
    [_tigerToken:protected] => 
    [_data:protected] => 
    [_timeout:protected] => 10
    [_cookieJar:protected] => 
    [dbh] => 
    [opts] => Array
        (
        )

)

1 个答案:

答案 0 :(得分:1)

你说的是REST类扩展(是子类)Parent。但是在类Parent中,您指的是子类中的方法。子类可以使用父方法,但父类无法访问其子类。扩展课程是一条单行道。

相关问题