名称空间和单例的PHP问题

名称空间和单例的PHP问题,php,namespaces,singleton,Php,Namespaces,Singleton,嘿,伙计们,我有一个问题,我想是因为名称空间的缘故,我有我的父类父类,在父类中我做一些事情,将它添加到$this,然后扩展到一个子类,希望$this能够继续,但是,除了父构造函数中明确提到的内容之外,里面的所有内容似乎都消失了($timeout=10),我正在试图找出我编写这段代码的原因,如果有人能向我解释为什么它不能像我想的那样工作 Namespace Services; Class TheParent { public function __construct($timeou

嘿,伙计们,我有一个问题,我想是因为名称空间的缘故,我有我的父类
父类
,在父类中我做一些事情,将它添加到
$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',
        );
    }
 }
这是返回的rest对象,您可能会认为我还拥有来自父对象的数据,在传递给rest之前,已经在父对象中定义了appKey等内容

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
        (
        )

)

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

看起来像
父对象
Services
命名空间中,而不是
Services\REST
。我刚刚纠正了这一点,我得到的响应发布在代码下面。加1表示在我之前捕捉到它=)我看到您添加了一个新的REST对象。您应该将其命名为与REST类不同的名称,否则会自找麻烦。该REST对象是使用getInstance()创建的,它是REST类的实例父类的用途是什么。只上一节休息课。谢谢你帮我弄明白我做错了什么。接受并加一。很高兴我能帮忙。我不确定我是。