I am studying how array_column
works and I have read that if applying this function to an array of objects whose properties are private/protected, an implementation of both __get
and __isset
is needed. But I don't understand why __isset
is used when __get
itself can access these properties.
<?php
class Person{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function __get($prop)
{
return $this->$prop;
}
public function __isset($prop) : bool
{
return isset($this->$prop);
}
}
$people = [
new Person('Fred'),
new Person('Jane'),
new Person('John'),
];
print_r(array_column($people, 'name'));
?>
I have found this related question, but I didn't find an answer.