Check variable is public php

后端 未结 3 1343
借酒劲吻你
借酒劲吻你 2021-01-29 10:49

I want to check if a local variable in a class is public or private. The reason is to create a function like this:

function ToArray() {
  $arr = array();
  forea         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 10:54

    From the PHP documentation,

    $foo = new Foo();
    
    $reflect = new ReflectionClass($foo);
    $props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
    
    foreach ($props as $prop) {
        print $prop->getName() . "\n";
    }
    
    var_dump($props);
    

提交回复
热议问题