How to make an object public?

前端 未结 3 1614
时光说笑
时光说笑 2021-01-27 20:08

how would I go about getting around the \"protected\" so I can output the data.

tabs\\api\\property\\Property Object ( 
    [id:protected] => 90_4_HH 
    [pr         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-27 20:23

    I did not fully understand your question, but if you want to access protected properties from outside the class, you have to use Reflection:

    $reflObj = new ReflectionObject($property);
    $props = $reflObj->getProperties(ReflectionProperty::IS_PROTECTED);
    
    foreach ($props as $prop) {
      $prop->setAccessible(true);
      echo $prop->getName() . ":" . $prop->getValue($property), "\n";
    }
    

    Sample for outputting the address:

    $reflObj = new ReflectionObject($property);
    $addrProp = $reflObj->getProperty('address');
    
    $addrProp->setAccessible(true);
    echo $addrProp->getValue($property);
    

提交回复
热议问题