如何在 PHP 中实现 __isset() 魔术方法?
我正在尝试使 empty()
和 isset()
等函数处理方法返回的数据。
到目前为止我所拥有的:
abstract class FooBase{
public function __isset($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
return isset($this->$getter()); // not working :(
// Fatal error: Can't use method return value in write context
}
public function __get($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
return $this->$getter();
}
public function __set($name, $value){
$setter = 'set'.ucfirst($name);
if(method_exists($this, $setter))
return $this->$setter($value);
}
public function __call($name, $arguments){
$caller = 'call'.ucfirst($name);
if(method_exists($this, $caller)) return $this->$caller($arguments);
}
}
用法:
class Foo extends FooBase{
private $my_stuff;
public function getStuff(){
return $this->my_stuff;
}
public function setStuff($stuff){
$this->my_stuff = $stuff;
}
}
$foo = new Foo();
if(empty($foo->stuff)) echo "empty() works! \n"; else "empty() doesn't work:( \n";
$foo->stuff = 'something';
if(empty($foo->stuff)) echo "empty() doesn't work:( \n"; else "empty() works! \n";
我怎样才能让它如此空/isset 返回 true/false 如果:
- 上面的
my_stuff
未设置,或者在empty()
- 方法不存在的情况下具有空值或零值(不确定)如果需要的话,因为我认为你会得到无论如何,这是一个致命错误)
?
I'm trying to make functions like empty()
and isset()
work with data returned by methods.
What I have so far:
abstract class FooBase{
public function __isset($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
return isset($this->$getter()); // not working :(
// Fatal error: Can't use method return value in write context
}
public function __get($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
return $this->$getter();
}
public function __set($name, $value){
$setter = 'set'.ucfirst($name);
if(method_exists($this, $setter))
return $this->$setter($value);
}
public function __call($name, $arguments){
$caller = 'call'.ucfirst($name);
if(method_exists($this, $caller)) return $this->$caller($arguments);
}
}
the usage:
class Foo extends FooBase{
private $my_stuff;
public function getStuff(){
return $this->my_stuff;
}
public function setStuff($stuff){
$this->my_stuff = $stuff;
}
}
$foo = new Foo();
if(empty($foo->stuff)) echo "empty() works! \n"; else "empty() doesn't work:( \n";
$foo->stuff = 'something';
if(empty($foo->stuff)) echo "empty() doesn't work:( \n"; else "empty() works! \n";
How can I make it so empty/isset return true/false if:
my_stuff
above is not set, or has a empty or zero value in case ofempty()
- the method doesn't exist (not sure if neeed, because I think you get a fatal error anyway)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查
$getter()
是否存在(如果不存在,则假定该属性也不存在)并返回一个非空值。因此NULL
将导致它返回 false,正如您在阅读 的 php 手册后所期望的那样isset()
。This check whether or not
$getter()
exists (if it does not exist, it's assumed that the property also does not exist) and returns a non-null value. SoNULL
will cause it to return false, as you would expect after reading the php manual forisset()
.多一点不依赖吸气剂的选择
A bit more option not to depend on getter
您的代码由于这些行而返回错误:
您可以将其替换为:
如果未定义 getter 或返回
NULL
,它将返回 false。我建议您应该更好地考虑确定某些属性是否已设置的方式。上面的代码还假设您正在为所有属性创建 getter,因此当没有 getter 时,该属性被假定为未设置。
另外,为什么要使用吸气剂?他们在这里似乎有点矫枉过正。
Your code returns error because of these lines:
You can just replace it with:
and it will return false if the getter is not defined or it returns
NULL
. I propose you should better think of the way you determine some property is set or not.The above code is also assuming that you are creating getters for all of your properties, thus when there is no getter, the property is assumed as not set.
Also, why are you using getters? They seem to be some overkill here.