gpt4 book ai didi

PHP 命名空间和单例问题

转载 作者:行者123 更新时间:2023-12-04 17:04:28 25 4
gpt4 key购买 nike

嘿伙计们,我遇到了一个问题,我认为是由于命名空间的原因,我有我的父类 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 之前已经定义了等等。
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 类的子类。但是在 Parent 类中,您指的是子类中的方法。子类可以使用父方法,但父类不能访问它们的子类。扩展一个类是一条单行道。

关于PHP 命名空间和单例问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16408675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com