我想知道是否可以简化我的PHP条件。
发件人:
/* Do nothing if there are no taxonomies. */
if(!property_exists(__CLASS__, 'taxonomies') || !$this->taxonomies || empty($this->taxonomies) || is_null($this->taxonomies)){
return;
}
收件人:
/* Do nothing if there are no taxonomies. */
if(!property_exists(__CLASS__, 'taxonomies') || !$this->taxonomies){
return;
}
!$this->taxonomies
是否也完成了!is_null($this->taxonomies)
和!empty($this->taxonomies)
?
- 必须存在类属性
- 数据不能为NULL
- 数据绝对不能为空
- 数据绝对不能有错误的值
- 是的,
!$this->taxonomies
检查$this->taxonomies
是否不包含评估为布尔false的值,该值包括empty string
、NULL
、FALSE
、ZERO int
、empty array
以及其他值 - 一般来说,
!property_exists(__CLASS__, 'taxonomies')
看起来是一个糟糕的设计(这取决于您正在做什么),但一般来说,您应该知道您正在使用哪个实例,以及它应该具有什么相应的接口。因此,应该定义taxonomies
,甚至应该有一个getter(例如getTaxonomies())来封装它。然后简单地检查if (!empty($obj->taxonomies))
或简单地(无论你喜欢什么)if(!$obj->taxonomies)
就足够了