How to make an object public?

前端 未结 3 1608
时光说笑
时光说笑 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条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-27 20:33

    Answer to the question posed by the title is that all classes are public.

    What you are asking is how to access protected member variables.

    Taken from here (http://ajmm.org/2011/06/using-php-reflection-to-read-a-protected-property/), this is an example of how to do this:

    public static function getReflectedPropertyValue($class, $propertyName)
    {
        $reflectedClass = new ReflectionClass($class);
        $property = $reflectedClass->getProperty($propertyName);
        $property->setAccessible(true);
    
        return $property->getValue($class);
    }
    
    ...
    
    getReflectedPropertyValue($yourObject, 'protectedProperty');
    

    That said, the question is why you want to do this. Members are marked protected specifically to prevent you from doing this. If you have access to the source code that defines this other class, then it might make more sense to either change these members to "public" or (better) to provide a getXYZ() method for whichever properties you want to access.

提交回复
热议问题