php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50031 get_called_class sometimes returns incorrect value
Submitted: 2009-10-29 04:45 UTC Modified: 2009-10-29 11:58 UTC
From: abhibeckert at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: abhibeckert at gmail dot com
New email:
PHP Version: OS:

 

 [2009-10-29 04:45 UTC] abhibeckert at gmail dot com
Description:
------------
If you declare class method without declaring it as static and then 
call it as static, under certain circumstances get_called_class() will 
return an incorrect result.

I realise it's technically incorrect to not declare a method as static, 
But some code needs to be backwards compatible with previous versions of 
PHP, where declaring a method as static will give a syntax error.

Reproduce code:
---------------
class foo {
  static public function test() { var_dump(get_called_class()); }
  public function testTwo() { var_dump(get_called_class()); }
}

class bar {
  public function test() { foo::test(); }
  public function testTwo() { foo::testTwo(); }
}

echo "outside a class\n";
foo::test();
bar::testTwo();

echo "in a class\n";
$bar = new bar();
$bar->test();
$bar->testTwo();

Expected result:
----------------
outside a class
foo
foo

in a class
foo
foo

Actual result:
--------------
outside a class
foo
foo

in a class
foo
bar

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-29 11:58 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

That's expected. 

You do a non-static call to a non-static method from a unrelated class using classname::method(), this will cause PHP to mess with the actual call context, so that $this in foo::testFoo will be an object of bar.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Sep 21 23:01:27 2024 UTC