Detect if an object property is private in PHP

后端 未结 8 1257
太阳男子
太阳男子 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 00:53

    Check this code from http://php.net/manual/reflectionclass.getproperties.php#93984

      public function listProperties() {
        $reflect = new ReflectionObject($this);
        foreach ($reflect->getProperties(ReflectionProperty::IS_PUBLIC /* + ReflectionProperty::IS_PROTECTED*/) as $prop) {
          print $prop->getName() . "\n";
        }
      }
    

提交回复
热议问题