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 对象?我想不是,但我不确定。
Take the following code as an example:
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"
Now, I'm trying to archive the same result but without have to write the xpto class. My guess is I should have to write something like this:
function xpto()
{
static $instance = null;
if (is_null($instance) === true)
{
$instance = new stdClass();
}
return $instance;
}
echo xpto()->haha; // doesn't work - obviously
Now, is it possible to add __get() magic functionality to the stdClass object? I guess not, but I'm not sure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是不可能的。您不能向 stdClass 添加任何内容。此外,与 Java 不同,Java 中每个对象都是 Object 的直接或间接子类,而 PHP 中的情况并非如此。
你真正想达到什么目的?你的问题听起来有点像“我想关上车门,但没有车”:-)。
No, it is not possible. You cannot add anything to stdClass. Also, unlike Java, where every object is a direct or indirect subclass of Object, this is not the case in PHP.
What are you really trying to achieve? Your question sounds a bit like "I want to close the door of my car, but without having a car" :-).
OP看起来他们正在尝试使用全局范围内的函数来实现单例模式,这可能不是正确的方法,但无论如何,关于Cassy的回答,“你不能向stdClass添加任何内容” - 这不是真的。
您可以简单地通过为 stdClass 赋值来向它们添加属性:
但是,我认为您需要 PHP 5.3+ 才能添加方法(匿名函数/闭包),在这种情况下您可能可以执行类似的操作下列。不过,我没有尝试过这个。但如果这确实有效,你能用神奇的 __get() 方法做同样的事情吗?更新: 正如评论中所指出的,你不能以这种方式动态添加方法。分配一个匿名函数(PHP 5.3+)就可以做到这一点,并且简单分配一个函数(严格来说是一个闭包对象) 公共财产。
The OP looks like they are trying to achieve a singleton pattern using a function in the global scope which is probably not the correct way to go, but anyway, regarding Cassy's answer, "You cannot add anything to stdClass" - this is not true.
You can add properties to the stdClass simply by assigning a value to them:
However, I think you need PHP 5.3+ in order to add methods (anonymous functions / closures), in which case you might be able to do something like the following. However, I've not tried this. But if this does work, can you do the same with the magic __get() method?UPDATE: As noted in the comments, you cannot dynamically add methods in this way. Assigning an anonymous function (PHP 5.3+) does just that and simply assigns a function (strictly a closure object) to a public property.