以下面的代码为例:PHP stdClass的()与__get()魔术方法
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的对象?我猜不是,但我不确定。
感谢卡西,我认为可能有一种晦涩的方式来创建某种类的lambda类,但我猜不是。谢谢您的意见。 =) –