This will throw an error:
class foo
{
var $bar;
public function getBar()
{
return $this->Bar; // beware of capital 'B': "Fatal: unknown property".
}
}
But this won't:
class foo
{
var $bar;
public function setBar($val)
{
$this->Bar = $val; // beware of capital 'B': silently defines a new prop "Bar"
}
}
How can I force PHP to throw errors in BOTH cases? I consider the second case more critical than the first (as it took me 2 hours to search for a d....ned typo in a property).
You can use magic methods
__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.
class foo
{
var $bar;
public function setBar($val)
{
$this->Bar = $val; // beware of capital 'B': silently defines a new prop "Bar"
}
public function __set($var, $val)
{
trigger_error("Property $var doesn't exists and cannot be set.", E_USER_ERROR);
}
public function __get($var)
{
trigger_error("Property $var doesn't exists and cannot be get.", E_USER_ERROR);
}
}
$obj = new foo();
$obj->setBar('a');
It will cast error
Fatal error: Property Bar doesn't exists and cannot be set. on line 13
You can set Error Levels according to PHP error levels
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With