php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47335 get_called_class() sometimes returning incorrect value
Submitted: 2009-02-07 14:54 UTC Modified: 2009-02-07 18:54 UTC
From: php at kennel17 dot co dot uk Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.0beta1 OS: Win32
Private report: No CVE-ID: None
 [2009-02-07 14:54 UTC] php at kennel17 dot co dot uk
Description:
------------
The new get_called_class() function returns the wrong class name when called from within a static method, when that function is called from an instantiated class method.

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

class MyClass {
	function GetClassName() {
		return get_called_class();
	}
}

class OtherClass {
	function Foo() {
		return MyClass::GetClassName();
	}
}

$c = new OtherClass;

print( MyClass::GetClassName() . "\n");
print( OtherClass::Foo() . "\n");
print( $c->Foo() . "\n");

?>

Expected result:
----------------
MyClass
MyClass
MyClass


Actual result:
--------------
MyClass
MyClass
OtherClass


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-07 16:21 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2009-02-07 18:22 UTC] php at kennel17 dot co dot uk
I checked the documentation[1] before posting, and according to what it says there, this is not bogus.  

The documentation simply states that get_called_class() "Gets the name of the class the static method is called in." In this example, the static method is in MyClass, so the function should return "MyClass".

Now, either this is a bug in get_called_class(), or it is a bug in the documentation.  Personally, this function has a lot less utility if you can't guarantee that it behaves as described below.  How else can you get the current class name?  If this is intended behaviour, then can you please point to something that documents the reason for this unintuitive response.


[1] http://www.php.net/manual/en/function.get-called-class.php
 [2009-02-07 18:54 UTC] colder@php.net
The problem is that in your last example, the call is not static since you're in an object context and the getClassName method is not defined as static. Turn E_STRICT reporting on and you'll get errors about that.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 08 14:01:33 2025 UTC