php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64744 Differentiate between member function call on a null and non-null, non-objects
Submitted: 2013-04-30 16:19 UTC Modified: 2014-06-09 02:21 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mgraham at etsy dot com Assigned: stas
Status: Closed Package: Class/Object related
PHP Version: 5.4.14 OS: Linux
Private report: No CVE-ID:
 [2013-04-30 16:19 UTC] mgraham at etsy dot com
Description:
------------
The error message for "Call to a member function on a non-object" is not as helpful as it could be. It would be really great if it would distinguish when the variable is null instead of another type of actual data such as an integer, string or array.

This is helpful so that when fixing this issues, one can tell from the logs if a null check is sufficient or if I need to find out where the wrong type of data is being created.

Test script:
---------------
<?php
$x = (isset($argv[1]) && $argv[1] == 'null') ? null : 'non-null-non-object';
$x->fail();


Expected result:
----------------
Null is a special kind of non-object, so if the variable is null, the error message should be different to indicate that it's null.


mgraham@vm:~/development/tmp$ php bug.php null

Fatal error: Call to a member function fail() on null in /home/mgraham/development/tmp/bug.php on line 3
mgraham@vm:~/development/tmp$ php bug.php 

Fatal error: Call to a member function fail() on a non-object in /home/mgraham/development/tmp/bug.php on line 3
mgraham@vm:~/development/tmp$ 


Even better would be to say in the error message what the type of the variable is for, integer, array, string, whatever.

Actual result:
--------------
When running this script, there is no information about what type of non-object it was.


mgraham@vm:~/development/tmp$ php bug.php null

Fatal error: Call to a member function fail() on a non-object in /home/mgraham/development/tmp/bug.php on line 3
mgraham@vm:~/development/tmp$ php bug.php 

Fatal error: Call to a member function fail() on a non-object in /home/mgraham/development/tmp/bug.php on line 3
mgraham@vm:~/development/tmp$ 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-03 14:57 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2013-05-03 14:57 UTC] laruence@php.net
I am not sure why this is important?

the error is a FATAL error, when it occurs, the only important reason is "it's not 
a object",  and as for what exactlly type it is, doesn't matter, and won't help 
much either...
 [2013-05-03 19:51 UTC] mgraham at etsy dot com
-Status: Feedback +Status: Open
 [2013-05-03 19:51 UTC] mgraham at etsy dot com
The reason it's useful is that knowing it's null, rather than "a string" 
provides a great deal more information about how to fix it.

null can be considered a related type to whatever object I'm expecting, whereas 
a string or integer or array are totally different types of objects.

Consider this in a language like C++ for example:
SomeClass and SomeClass * are related types, but vector< int > is completely 
unrelated. If I get null for SomeClass *, then it's just an invalid value of 
type SomeClass *. I can check for that value and be done. If I'm getting another 
type altogether, then I have some other kind of problem that requires additional 
investigation.

The reason this is important is that it distinguishes between whether my 
variable is of the right type, but has the wrong value, or is it not even of the 
right type. Knowing that would make a lot of these kinds of errors much easier 
to fix.
 [2014-06-09 02:21 UTC] stas@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: stas
 [2014-06-09 02:21 UTC] stas@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Wed Feb 22 17:01:37 2017 UTC