Demonstration
class Student {
public $name;
private $age;
public function __construct($Name, $Age) {
$this->name = $Name;
$this->age = $Age;
}
}
$student = new Student('Alice', 21);
var_dump( obj::get($student, 'name') );
var_dump( obj::get($student, 'age') );
var_dump( obj::get($student, 'age', 'Secret!') );
string(5) "Alice"
NULL
string(7) "Secret!"
Calling a getter method
class Student {
public $name;
private $age;
public function __construct($Name, $Age) {
$this->name = $Name;
$this->age = $Age;
}
public function getAge() {
return $this->age;
}
}
$student = new Student('Alice', 21);
var_dump( obj::get($student, 'name') );
var_dump( obj::get($student, 'age') );
var_dump( obj::get($student, 'age', 'Secret!') );
string(5) "Alice"
int(21)
int(21)