php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34421 get_class and debug_backtrace report wrong class for inherited static methods
Submitted: 2005-09-08 09:34 UTC Modified: 2016-10-16 12:17 UTC
Votes:23
Avg. Score:4.8 ± 0.5
Reproduced:23 of 23 (100.0%)
Same Version:9 (39.1%)
Same OS:3 (13.0%)
From: wkonkel at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.0.5 OS: redhat
Private report: No CVE-ID: None
 [2005-09-08 09:34 UTC] wkonkel at gmail dot com
Description:
------------
When a static function is called in the scope of an extended class, the static function still thinks it's being called in the scope of the base class.

I found a similar bug at http://bugs.php.net/bug.php?id=30828 which was fixed in 5.0.5, but the problem still remains.

Reproduce code:
---------------
<?

class baseClass {
	static function do_get_class() {
		return get_class();
	}
	static function do_backtrace() {
		$backtrace = debug_backtrace();
		return $backtrace[0]['class'];
	}
}
	
class extendedBaseClass extends baseClass {
}

echo extendedBaseClass::do_get_class() . "<br>";
echo extendedBaseClass::do_backtrace() . "<br>";

?>

Expected result:
----------------
extendedBaseClass
extendedBaseClass

Actual result:
--------------
baseClass
baseClass

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-08 16:06 UTC] wkonkel at gmail dot com
Although this is how it currently does work, this is not logical. If I specify that the scope to be "extendedBaseClass", then why is the scope "baseClass"?  If this was not a static method, calling get_class() would return "extendedBaseClass" because the object simply isn't a "baseClass", it's an "extendedBaseClass".  Static methods are no different.
 [2005-09-09 13:25 UTC] johannes@php.net
This is a limitation in the way the engine works and it 
most likely won't change in the near future. See also 
previous discussions about it. 
http://zend.com/zend/week/week219.php#Heading5 
 [2005-12-22 10:42 UTC] thomas at gielfeldt dot dk
Hi

I've mailed the patch to the internals mailing list, hoping it would be taken into consideration.

two new functions have been added: get_scope() and is_static().

Example of solution to the above code after patching:

<?

class baseClass {
	static function do_get_scope() {
		return get_scope();
	}
	static function do_backtrace() {
		$backtrace = debug_backtrace();
		return $backtrace[0]['scope'];
	}
}
	
class extendedBaseClass extends baseClass {
}

echo extendedBaseClass::do_get_scope() . "<br>";
echo extendedBaseClass::do_backtrace() . "<br>";

?>

Expected result:
----------------
extendedBaseClass
extendedBaseClass

Actual result:
--------------
extendedBaseClass
extendedBaseClass
 [2005-12-22 12:05 UTC] thomas at gielfeldt dot dk
a grammatical correction: "hoping it would be" = "hoping it will be" 

:-)

/Thomas
 [2016-10-16 12:17 UTC] nikic@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: nikic
 [2016-10-16 12:17 UTC] nikic@php.net
This should be resolved as of PHP 5.3 with the introduction of late static binding. In particular get_class() would be get_called_class() or something involving "static".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 02:01:28 2024 UTC