【发布时间】:2010-12-26 03:27:34
【问题描述】:
以如下代码为例:
class xpto
{
public function __get($key)
{
return $key;
}
}
function xpto()
{
static $instance = null;
if (is_null($instance) === true)
{
$instance = new xpto();
}
return $instance;
}
echo xpto()->haha; // returns "haha"
现在,我正在尝试归档相同的结果,但不必编写 xpto 类。我的猜测是我应该写这样的东西:
function xpto()
{
static $instance = null;
if (is_null($instance) === true)
{
$instance = new stdClass();
}
return $instance;
}
echo xpto()->haha; // doesn't work - obviously
现在,是否可以将 __get() 魔术功能添加到 stdClass 对象?我猜不是,但我不确定。
【问题讨论】:
标签: php object singleton stdclass