【问题标题】:PHP: how to check if an object's properties have values?PHP:如何检查对象的属性是否有值?
【发布时间】:2013-10-20 12:14:41
【问题描述】:

我用它来检查一个对象是否有属性,

function objectHasProperty($input){
        return (is_object($input) && (count(get_object_vars($input)) > 0)) ? true : false;
 }

但是我想进一步检查以确保所有属性都有值,例如,

stdClass Object
        (
            [package] => 
            [structure] => 
            [app] => 
            [style] => 
            [js] => 
        )

如果所有属性都有空值,我想返回false。可能吗?有什么提示和想法吗?

【问题讨论】:

    标签: php stdclass php-5.4


    【解决方案1】:

    对于深入检查和更高级的处理,我会选择以下易于扩展的内容。将其视为对 dev-null-dweller 回答的后续回答(这是完全有效且很好的解决方案)。

    /**
     * Deep inspection of <var>$input</var> object.
     *
     * @param mixed $input
     *   The variable to inspect.
     * @param int $visibility [optional]
     *   The visibility of the properties that should be inspected, defaults to <code>ReflectionProperty::IS_PUBLIC</code>.
     * @return boolean
     *   <code>FALSE</code> if <var>$input</var> was no object or if any property of the object has a value other than:
     *   <code>NULL</code>, <code>""</code>, or <code>[]</code>.
     */
    function object_has_properties($input, $visibility = ReflectionProperty::IS_PUBLIC) {
      set_error_handler(function(){}, E_WARNING);
      if (is_object($input)) {
        $properties = (new ReflectionClass($input))->getProperties($visibility);
        $c = count($properties);
        for ($i = 0; $i < $c; ++$i) {
          $properties[$i]->setAccessible(true);
          // Might trigger a warning!
          $value = $properties[$i]->getValue($input);
          if (isset($value) && $value !== "" && $value !== []) {
            restore_error_handler();
            return true;
          }
        }
      }
      restore_error_handler();
      return false;
    }
    
    // Some tests
    
    // The bad boy that emits a E_WARNING
    var_dump(object_has_properties(new \mysqli())); // boolean(true)
    
    var_dump(object_has_properties(new \stdClass())); // boolean(false)
    
    var_dump(object_has_properties("")); // boolean(false)
    
    class Foo {
    
      public $prop1;
    
      public $prop2;
    
    }
    
    var_dump(object_has_properties(new Foo())); // boolean(false)
    
    $foo = new Foo();
    $foo->prop1 = "bar";
    var_dump(object_has_properties($foo)); // boolean(true)
    

    【讨论】:

      【解决方案2】:

      根据您认为什么是“空值”,您可能已经调整了删除不需要的值的回调函数:

      function objectHasProperty($input){
          return (
              is_object($input) 
              && 
              array_filter(
                  get_object_vars($input), 
                  function($val){ 
                      // remove empty strings and null values
                      return (is_string($val) && strlen($val))||($val!==null); 
                  }
              )
          ) ? true : false;
      }
      
      $y = new stdClass;
      $y->zero = 0;
      
      $n = new stdClass;
      $n->notset = null;
      
      var_dump(objectHasProperty($y),objectHasProperty($n));//true,false
      

      【讨论】:

      • 如果您只想检查null,您可以使用isset()。当然,对于""array(),这将返回true。因此,在这种情况下,您必须创建自己的支票(就像您所做的那样)。
      • 不确定我是否理解正确 - 你的意思是将empty 作为回调传递?
      • 就像乔治布莱顿在他的回答中所做的那样,这可以按预期工作。为了深入检查所有属性,我会选择循环,因为它比其他任何方法都更有效。
      • 这就是为什么我将答案留给其他要求更高的观众。
      【解决方案3】:

      有几种方法可以做到这一点,一直到使用 PHP 的反射 API,但要简单地检查对象的所有公共属性是否为空,您可以这样做:

      $properties = array_filter(get_object_vars($object));
      return !empty($properties);
      

      (临时变量 $properties 是必需的,因为您使用的是 PHP 5.4。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-11
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 2012-02-02
        • 1970-01-01
        • 2015-08-24
        相关资源
        最近更新 更多