Doc #52605 [Com]: get_class(NULL) returns class name when called insid member function of a class
| From: | gordon dot mcvey at ntlworld dot com | Date: | Tue, 28 Aug 2012 12:03:07 +0000 |
| Subject: | Doc #52605 [Com]: get_class(NULL) returns class name when called insid member function of a class | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8779@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=52605&edit=1
ID: 52605
Comment by: gordon dot mcvey at ntlworld dot com
Reported by: rahul dot pache at gmail dot com
Summary: get_class(NULL) returns class name when called insid
member function of a class
Status: Closed
Type: Documentation Problem
Package: Documentation problem
Operating System: windows 7
PHP Version: 5.3.3
Assigned To: degeberg
Block user comment: N
Private report: N
New Comment:
It may be behaving as described in the documentation, but it's seriously a big
misfeature! I was trying to figure out why get_class was returning the name of
the class it was being called in and not the class of the object I was passing
into it for the best part of a morning before I realised that the variable I was
passing in had failed to instantiate and therefore had a NULL value.
This function has inconsistent behaviour, and it should be updated to make the
argument mandatory, and not assume that a NULL value means the name of the
containing class.
Previous Comments:
------------------------------------------------------------------------
[2010-08-15 11:09:51] degeberg@php.net
There is no issue except in the documentation.
Re 1: Because that is only allowed inside a class.
Re 2: Because that is what it defaults to.
Re 3: Undefined variables default to NULL. You're also getting an E_NOTICE telling you that
this might be a problem. Not a bug in PHP.
I actually thought the documentation already addressed point 1 and 2, but looking at it again it
appears it doesn't. I've committed one more fix.
------------------------------------------------------------------------
[2010-08-15 11:09:44] degeberg@php.net
Automatic comment from SVN on behalf of degeberg
Revision: http://svn.php.net/viewvc/?view=revision&revision=302261
Log: Clarified default behavior when omitting param. See PHP bug #52605.
------------------------------------------------------------------------
[2010-08-15 10:48:02] rahul dot pache at gmail dot com
<?php
get_class(NULL);
?>
Output:
Warning: get_class() called without object from outside a class in
D:\EasyPHP\www\index.php on line 2
Comment:
1. Then why is get_class(NULL) when called from outside the class returning a
warning message.
2. I don't understand why is it returning class name "foo" when null is passed
to it inside member function of a class.
3. I made a function to check if the argument passed is object of same class or
not.
<?php
class foo {
public static check($obj) {
if(get_class($obj) == __CLASS__) {
echo "YES";
} else {
echo "NO";
}
}
}
$var = new foo();
foo::check($var); // This will print YES
foo::check($uninitialized_variable)
/*
This will also print YES because NULL is passed to get_class() in the function
foo::check(). And when NULL is passed to a get_class inside a member function of
the class it returns class name.
*/
?>
I think this is not a documentation problem, there is some serious bug.
:(
------------------------------------------------------------------------
[2010-08-15 09:17:37] degeberg@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation better.
------------------------------------------------------------------------
[2010-08-15 09:17:25] degeberg@php.net
Automatic comment from SVN on behalf of degeberg
Revision: http://svn.php.net/viewvc/?view=revision&revision=302255
Log: Fixed PHP bug #52605 (get_class(NULL) returns class name when called insid member function of a
class)
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=52605
--
Edit this bug report at https://bugs.php.net/bug.php?id=52605&edit=1