Accessing StdClass value with colon :protected

后端 未结 4 897
旧巷少年郎
旧巷少年郎 2021-01-26 03:11

How do I access the value of stdClass with colon \":protected\"?

For example, I had this $obj with these result :

object(Google_Service_Plus_PeopleFeed)         


        
4条回答
  •  执念已碎
    2021-01-26 03:58

    Please note that there is probably a reason that these properties are protected, so you should think twice before trying to access them.

    If you need to access protected variables, you could use Reflection, but there might be an easier solution. By binding a closure to the object, you should be able to access the protected variables from the closure:

    class X {
       protected $a = 10;
       public $b = 20; 
    }
    
    
    $closure = function() {
              return get_object_vars($this);
    };
    
    $result = Closure::bind($closure, new X(), 'X');
    var_dump($result()); 
    

提交回复
热议问题