jeudi 22 août 2019

ReflectionProperty constructor

I'll be grateful (and wiser at the same time ;) ) if anyone can explain me below behavior:

We've got a class.

class Test {
    public $id;
    private $name;
    protected $color;
}

And we've got ReflectionProperty constructor behavior that I do not fully understand.

Working one first:

function check() {
    $class = new Test();
    $ref = new ReflectionObject($class);
    $pros = $ref->getProperties();
    foreach ($pros as $pro) {
        false && $pro = new ReflectionProperty();
        print_r($pro);
    }
}

This will give correct output of:

ReflectionProperty Object
(
    [name] => id
    [class] => Test
)
ReflectionProperty Object
(
    [name] => name
    [class] => Test
)
ReflectionProperty Object
(
    [name] => color
    [class] => Test
)

Now: if I remove "false" from this line:

false && $pro = new ReflectionProperty();

the output will be:

PHP Fatal error:  Uncaught ArgumentCountError: ReflectionProperty::__construct() expects exactly 2 parameters, 0 given

ReflectionProperty::__construct() takes ($class, $name)

The question therefore is: why 'false' even works in the first place?





Aucun commentaire:

Enregistrer un commentaire