php判断对象是否存在属性_PHP: property_exists - Manual

declared properties cannot be unset

any set property does exist, even being set to null, regardless how it was set

public$my_public;

protected$my_protected;

private$my_private;

function__construct() {$this->dumper('before-constructed');$this->my_constructed_int=123;$this->my_constructed_null=null;$this->dumper('after-constructed');

}

public functiondumper($name) {printf("\n[%s] dump:\n",$name);

foreach ($this->my_checklist() as$prop) {printf("[%s]:\t",$prop);var_dump(property_exists($this,$prop));

}

}

public functionunset_all() {

foreach ($this->my_checklist() as$prop) {

unset($this->$prop);

}

}

private functionmy_checklist() {

return array('my_public','my_protected','my_private','my_constructed_int','my_constructed_null','my_assigned_int','my_assigned_null',);

}

}$object= newdemo_property_exists();$object->dumper('before-assigned');$object->my_assigned_int=456;$object->my_assigned_null=null;$object->dumper('after-assigned');$object->unset_all();$object->dumper('after-unset');/*

results:

[before-constructed] dump:

[my_public]:    bool(true)

[my_protected]: bool(true)

[my_private]:   bool(true)

[my_constructed_int]:   bool(false)

[my_constructed_null]:  bool(false)

[my_assigned_int]:      bool(false)

[my_assigned_null]:     bool(false)

[after-constructed] dump:

[my_public]:    bool(true)

[my_protected]: bool(true)

[my_private]:   bool(true)

[my_constructed_int]:   bool(true)

[my_constructed_null]:  bool(true)

[my_assigned_int]:      bool(false)

[my_assigned_null]:     bool(false)

[before-assigned] dump:

[my_public]:    bool(true)

[my_protected]: bool(true)

[my_private]:   bool(true)

[my_constructed_int]:   bool(true)

[my_constructed_null]:  bool(true)

[my_assigned_int]:      bool(false)

[my_assigned_null]:     bool(false)

[after-assigned] dump:

[my_public]:    bool(true)

[my_protected]: bool(true)

[my_private]:   bool(true)

[my_constructed_int]:   bool(true)

[my_constructed_null]:  bool(true)

[my_assigned_int]:      bool(true)

[my_assigned_null]:     bool(true)

[after-unset] dump:

[my_public]:    bool(true)

[my_protected]: bool(true)

[my_private]:   bool(true)

[my_constructed_int]:   bool(false)

[my_constructed_null]:  bool(false)

[my_assigned_int]:      bool(false)

[my_assigned_null]:     bool(false)

*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值