doupo1865 2013-05-06 23:03
浏览 43
已采纳

PHP与命名空间和单例有关

Hey guys I'm having an issue that I think is because due to namespaces, I have my parent class TheParent where I do some stuff, add it to $this then extend to a child class expecting that $this will carry over, but everything inside except what is explicitly mentioned in the parents constructor seems to vanish (the $timeout = 10) I'm trying to figure out where I borked this code at, and if anyone could explain it to me why this is not working like I would think it should?

 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',
        );
    }
 }

This is the rest object returned, you would THINK that I would also have data from TheParent which is where things like _appKey etcetera have already been defined before being passed to REST

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条回答 默认 最新

  • duanguochi6194 2013-05-06 23:09
    关注

    You are saying that class REST extends (is a child of) class Parent. But in class Parent you are referring to methods in the child class. Child classes can use parent methods but parent classes have no access to their child classes. Extending a class is a one way street.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源