命名空间和单例的 PHP 问题


PHP issues with namespaces and singletons

嘿伙计们,我遇到了一个问题,我认为这是因为由于命名空间,我有我的父类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的数据,这是在传递给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 扩展(是(类父级的子级。但是在类父类中,您指的是子类中的方法。子类可以使用父方法,但父类无法访问其子类。扩展类是一条单行道。