php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52605 get_class(NULL) returns class name when called insid member function of a class
Submitted: 2010-08-14 23:31 UTC Modified: 2010-08-15 11:09 UTC
From: rahul dot pache at gmail dot com Assigned: degeberg (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.3 OS: windows 7
Private report: No CVE-ID: None
 [2010-08-14 23:31 UTC] rahul dot pache at gmail dot com
Description:
------------
get_class(NULL) returns class name when called inside member function of a 
class.

<?php
CLASS foo {
	public static function bar() {
		echo get_class(NULL);
	}
	public function baz() {
		echo get_class(NULL);
	}
}
foo::bar();
echo "<br>";
$f = new foo();
$f->baz();
?>
output:
foo
foo

Expected output:
Warning: get_class() called without object from outside a class in filename.php 
on line 4
Warning: get_class() called without object from outside a class in filename.php 
on line 7


-------Message--------
When some string/int is sent an expected output is produced that is a warning 
message.
I am using EasyPHP 5.3.3. This error does not exists in PHP 5.2.6
----------------------


------phpinfo()------- [just first table]
System	Windows NT RAHUL-PC 6.1 build 7600 (Unknow Windows version Ultimate 
Edition) i586
Build Date	Jul 21 2010 20:00:47
Compiler	MSVC6 (Visual C++ 6.0)
Architecture	x86
Configure Command	cscript /nologo configure.js "--enable-snapshot-build" 
"--disable-isapi" "--enable-debug-pack" "--disable-isapi" "--without-mssql" "--
without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=D:\php-
sdk\oracle\instantclient10\sdk,shared" "--with-oci8=D:\php-
sdk\oracle\instantclient10\sdk,shared" "--with-oci8-11g=D:\php-
sdk\oracle\instantclient11\sdk,shared" "--enable-object-out-dir=../obj/" "--
enable-com-dotnet" "--with-mcrypt=static"
Server API	Apache 2.0 Handler
Virtual Directory Support	enabled
Configuration File (php.ini) Path	C:\Windows
Loaded Configuration File	D:\EasyPHP\apache\php.ini
Scan this dir for additional .ini files	(none)
Additional .ini files parsed	(none)
PHP API	20090626
PHP Extension	20090626
Zend Extension	220090626
Zend Extension Build	API220090626,TS,VC6
PHP Extension Build	API20090626,TS,VC6
Debug Build	no
Thread Safety	enabled
Zend Memory Manager	enabled
Zend Multibyte Support	disabled
IPv6 Support	enabled
Registered PHP Streams	php, file, glob, data, http, ftp, zip, compress.zlib, 
compress.bzip2, phar
Registered Stream Socket Transports	tcp, udp
Registered Stream Filters	convert.iconv.*, mcrypt.*, mdecrypt.*, 
string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, 
consumed, dechunk, zlib.*, bzip2.*

Test script:
---------------
<?php
CLASS foo {
	public static function bar() {
		echo get_class(NULL);
	}
	public function baz() {
		echo get_class(NULL);
	}
}
foo::bar();
echo "<br>";
$f = new foo();
$f->baz();
?>

Expected result:
----------------
Warning: get_class() called without object from outside a class in filename.php on 
line 4
Warning: get_class() called without object from outside a class in filename.php on 
line 7

Actual result:
--------------
foo
foo

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-15 04:06 UTC] cataphract@php.net
-Type: Bug +Type: Documentation Problem
 [2010-08-15 04:06 UTC] cataphract@php.net
This is actually a documentation problem.

The convention is to give internal functions that have optional arguments a default value and have them behave the same way when the default value is given and when the argument's not passed at all.

In PHP 5.2, you could not pass an argument to get_class and have it have the same behavior as when nothing is passed. This was fixed in PHP 5.3 by making "NULL" the default argument value.
 [2010-08-15 04:07 UTC] cataphract@php.net
-Package: *General Issues +Package: Documentation problem
 [2010-08-15 09:17 UTC] degeberg@php.net
Automatic comment from SVN on behalf of degeberg
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=302255
Log: Fixed PHP bug #52605 (get_class(NULL) returns class name when called insid member function of a class)
 [2010-08-15 09:17 UTC] degeberg@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: degeberg
 [2010-08-15 09:17 UTC] 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 10:48 UTC] 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 11:09 UTC] degeberg@php.net
Automatic comment from SVN on behalf of degeberg
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=302261
Log: Clarified default behavior when omitting param. See PHP bug #52605.
 [2010-08-15 11:09 UTC] 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.
 [2012-08-28 12:03 UTC] gordon dot mcvey at ntlworld dot com
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.
 [2013-06-13 18:26 UTC] tgabi333 at gmail dot com
Agree with Gordon, +1 for a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 00:01:41 2024 UTC