如何最小化PHP条件用法


How to Minify PHP Conditional Usages?

我想知道是否可以简化我的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
  • 数据绝对不能为空
  • 数据绝对不能有错误的值
  1. 是的,!$this->taxonomies检查$this->taxonomies是否不包含评估为布尔false的值,该值包括empty stringNULLFALSEZERO intempty array以及其他值
  2. 一般来说,!property_exists(__CLASS__, 'taxonomies')看起来是一个糟糕的设计(这取决于您正在做什么),但一般来说,您应该知道您正在使用哪个实例,以及它应该具有什么相应的接口。因此,应该定义taxonomies,甚至应该有一个getter(例如getTaxonomies())来封装它。然后简单地检查if (!empty($obj->taxonomies))或简单地(无论你喜欢什么)if(!$obj->taxonomies)就足够了