列举php magic方法,如何在PHP中實現__isset()魔術方法?

I'm trying to make functions like empty() and isset() work with data returned by methods.

我正在嘗試使用empty()和isset()等函數處理方法返回的數據。

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";

http://codepad.org/QuPNLYXP

How can I make it so empty/isset return true/false if:

如果符合以下情況,我怎樣才能使它為空/ isset返回true / false:

my_stuff above is not set, or has a empty or zero value in case of empty()

上面的my_stuff未設置,或者在empty()的情況下為空或零值

the method doesn't exist (not sure if neeed, because I think you get a fatal error anyway)

該方法不存在(不確定是否需要,因為我認為你得到一個致命的錯誤)

?

3 个解决方案

#1

7

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().

這將檢查$ getter()是否存在(如果它不存在,則假定該屬性也不存在)並返回非null值。所以NULL會導致它返回false,正如您在閱讀php手冊中的isset()之后所期望的那樣。

#2

3

A bit more option not to depend on getter

更多選擇不依賴於吸氣劑

public function __isset($name)

{

$getter = 'get' . ucfirst($name);

if (method_exists($this, $getter)) {

return !is_null($this->$getter());

} else {

return isset($this->$name);

}

}

#3

1

Your code returns error because of these lines:

您的代碼因以下行而返回錯誤:

if(method_exists($this, $getter))

return isset($this->$getter());

You can just replace it with:

您可以將其替換為:

if (!method_exists($this), $getter) {

return false; // method does not exist, assume no property

}

$getter_result = $this->$getter();

return isset($getter_result);

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.

如果未定義getter或返回NULL,它將返回false。我建議你最好考慮一下你確定某些屬性的設置方式。

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.

上面的代碼也假設您正在為所有屬性創建getter,因此當沒有getter時,該屬性被假定為未設置。

Also, why are you using getters? They seem to be some overkill here.

另外,你為什么要使用吸氣劑?他們似乎有點矯枉過正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值