Calling method on non-object recoverable? (was: type hinting throwing a fatal error)
| From: | Michael Sims | Date: | Thu, 25 Aug 2005 14:58:01 +0000 |
| Subject: | Calling method on non-object recoverable? (was: type hinting throwing a fatal error) | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-18430@lists.php.net to get a copy of this message | ||
Derick Rethans wrote:
> I implemented that (except I dropped the _ERROR part for
> E_RECOVERABLE_ERROR as I found it too long). The patch is here:
>
> http://files.derickrethans.nl/patches/e_fatal-20050825.diff.txt
>
> Any comments?
Since this new error level is being created, I have a suggestion for another type of
error I would very much like to see changed from fatal to "recoverable". The error
I'm referring to is attempting to call a method on a non-object. This used to be a
E_WARNING a long time ago in PHP 4 (if memory serves), and is now fatal.
I'm sure that many of you believe this SHOULD be a fatal error, but I find that this
does tend to come up when dealing with methods that return either an object OR
null/false. Take the following example when dealing with data access objects.
Suppose that $user is an instance of User and has a method getLocation() that will
either return an instance of Location or null if the user is not assigned to any
location:
print $user->getLocation()->getPhoneNumber();
If getLocation() returns null then the above is a fatal error. I realize that this
is a mistake, and that the return value from getLocation() should be checked before
calling getPhoneNumber() on it, but I've found this has been a typical error in my
and my coworkers code.
I'm proposing that this become an E_RECOVERABLE_ERROR so that I can at least catch
it with my custom error handler and shutdown the script gracefully. Any thoughts?