How the php reflection class gets the private properties obtained by the constructor

class test{
    private $a=array();
    public function __construct() {
        $this->geta ();
    }
    private function geta(){
        $b [] = 1;
        $b [] = 2;
        
        $this->a = $b;
        // return
    }
}


//
$ref_class = new ReflectionClass("test");
$geta= $ref_class->newInstance();
$method = $ref_class->getmethod("geta");
$method->setAccessible(true);
$a = $method->invoke($rongyuclass); //geta

there is such a class. Now the private method geta does not return any data, but the constructor assigns a value to the private attribute a. When I directly use reflection to get the variable $a, I can only get a null value. How to perform the construction first and then get the assigned private attribute a?

Php
Mar.03,2021

$ref = new ReflectionClass ('test');
$a = $ref- > getProperty (' a');
$a-> setAccessible (true); / / set access to attribute a
var_dump ($a-> getValue (new test));


class test{
    private $a=array();
    public function __construct() {
        $this->geta ();
    }
    private function geta(){
        $b [] = 1;
        $b [] = 2;
        
        $this->a = $b;
        // return
    }
}

$test = new test;

$closure = function() {
    return $this->a;
};
// rebinds the closure to the $test object
$closure  = $closure->bindTo($test, $test);
print_r($closure());
Menu