- use
$this->parameters[$property]
instead of$parameters[$property]
in the getter - call it
$user->username
$parameters
should be private to keep the encapsulation
In code
class User {
private $parameters; // should be private (encapsulation)
public function __construct($parameters)
{
$this->parameters= $parameters;
}
public function __get($property) {
return $this->parameters[$property]; // instead of $parameters[$property]
}
}
$params = array(
"username" => "John Doe",
"age" => 33
);
$user = new User($params);
echo $user->username; // -> instead of .