Detect if an object property is private in PHP

后端 未结 8 1259
太阳男子
太阳男子 2021-02-04 00:24

I\'m trying to make a PHP (5) object that can iterate through its properties, building an SQL query based only on its public properties, not its private ones.

As this pa

相关标签:
8条回答
  • 2021-02-04 01:05
    $propertyName = 'bar';
    
    if(in_array(propertyName, array_keys(get_class_vars(get_class($yourObject))))) {
    
    }
    
    0 讨论(0)
  • 2021-02-04 01:07

    You can easily use the Reflection API to check the visibility of properties:

    $rp = new \ReflectionProperty($object, $property);
    if ($rp->isPrivate) {
      // Do if the property is private
    } else {
      // Do if the property is public or protected
    }
    
    0 讨论(0)
提交回复
热议问题