Once I saw someone using the following code for __get method and it seemed elegant so I copied.
public function __get($param)
{
if (!isset($this->params[$param])) {
throw new Exception("Property doesn't exist");
}
return $this->params[$param];
}
But now it seems too much to handle an exception when doing a simple action based on whether the property exists or not.
What is the better thing to do in this case. is returning NULL and then checking enough?
In php, this isn’t an exceptional circumstance, unlike some other languages. I agree with you in that an exception is too dramatic unless there’s special circumstances at play.
I would tend to mimic the expected behavior that is normal in php, which is to return the value of null and trigger an error of level
E_NOTICEorE_USER_NOTICE. This is the normal behavior when you try to read an undefined property, array indice, or variable.btw- you might also be intrested in implementing the magic __isset method