我正在尝试使用像empty()和isset()这样的函数使用方法返回的数据.
原文链接:https://www.f2er.com/php/131657.html我到目前为止
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以上没有设置,或者在空()的情况下有一个空值或零值
>该方法不存在(不知道如果neeed,因为我认为你得到一个致命的错误)
?