Daniel Trebbien
Daniel Trebbien

Reputation: 39208

Is it possible to turn "Notice: Trying to get property of non-object" into a Fatal Error?

If a variable is holding NULL in PHP, attempting to access a property on it results in the notice "Trying to get property of non-object". If an attempt is made to call a method on it, however, the result is "Fatal error: Call to a member function method-name() on a non-object":

<?php
$obj = NULL;
var_dump($obj->prop);
$obj->method();

http://codepad.org/HZyYd12A

Is there a way to make PHP treat both of these runtime errors as Fatal Errors?

Upvotes: 1

Views: 511

Answers (2)

Ahmad Hajjar
Ahmad Hajjar

Reputation: 1853

I don't have an idea but i think you can handle it manualy by catching the notice and throwing an exception followed by the line

exit();

;)

Upvotes: 1

deceze
deceze

Reputation: 522210

You can register your own error handler, which can promote this kind of error to a fatal error.
See http://php.net/set_error_handler and specifically the examples.

Upvotes: 1

Related Questions