如何在PHP中实现__isset()魔术方法? [英] How to implement __isset() magic method in PHP?
本文介绍了如何在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 :(
//致命错误:在写上下文中不能使用方法返回值
}
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);
}
} b $ b
用法:
code> class Foo extends FooBase {
private $ my_stuff;
public function getStuff(){
return $ this-> my_stuff;
}
public function setStuff($ stuff){
$ this-> my_stuff = $ stuff;
}
}
$ b b $ foo = new Foo();
if(empty($ foo-> stuff))echoempty()works!\\\
; elseempty()不工作(\ n;
$ foo-> stuff ='something';
if(empty($ foo-> stuff))echoempty ()不工作:( \\\
; elseempty()works!\\\
;
$ b b
如何I make it so empty / isset return true / false if:
-
my_stuff
未设置,或在empty()
- 的情况下为空或零值该方法不存在如果是neeed,因为我认为你得到一个致命错误反正)
?
解决方案
public function __isset($ name){
$ getter ='get'.ucfirst($ name);
return method_exists($ this,$ getter)&&!is_null($ this-> $ getter());
}
这会检查
$ getter()
是否存在(如果不存在,则假定属性不存在)并返回非空值。因此,NULL
将导致它返回false,正如您在阅读php手册isset()
。I'm trying to make functions like
empty()
andisset()
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)
?
解决方案
public function __isset($name){
$getter = 'get'.ucfirst($name);
return method_exists($this, $getter) && !is_null($this->$getter());
}
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. So NULL
will cause it to return false, as you would expect after reading the php manual for isset()
.
这篇关于如何在PHP中实现__isset()魔术方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文