|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61033 __FUNCTION__ doesn't report correctly in alias trait methods
Submitted: 2012-02-09 20:23 UTC Modified: 2012-02-09 20:44 UTC
From: marc at easen dot co dot uk Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 5.4.0RC7 OS: Gentoo
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
2 + 3 = ?
Subscribe to this entry?

 [2012-02-09 20:23 UTC] marc at easen dot co dot uk
The __FUNCTION__ magic constant does not report correctly in aliased methods 
within traits.

When a trait function is called by it's initial name __FUNCTION__ is correct. 
When it is called by it aliased name __FUNCTION__ still reports as the initial 
name, but debug_backtrace() reports the aliased method name.

Test script:
trait MyTrait {
	public function foo()
		$backtrace = debug_backtrace();
		echo '__FUNCTION__ = ' . __FUNCTION__ . PHP_EOL;
		echo '$backtrace[0][\'function\']) = ' . $backtrace[0]['function'] . PHP_EOL;

class MyClass {
	use MyTrait { foo as public bar; }


$instance = new MyClass();
echo 'foo()' . PHP_EOL;
echo PHP_EOL;
echo 'bar()' . PHP_EOL;

Expected result:
__FUNCTION__ = foo
$backtrace[0]['function']) = foo

__FUNCTION__ = bar
$backtrace[0]['function']) = bar

Actual result:
__FUNCTION__ = foo
$backtrace[0]['function']) = foo

__FUNCTION__ = foo
$backtrace[0]['function']) = bar


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-09 20:43 UTC]
While it made sense to change __CLASS__ to report the using class (since a trait is 
not a class), I do not think that it makes much sense to change __FUNCTION__ to do 
such magic.

__FUNCTION__ referes here to the compile time name of the lexical entity the magic 
constant is embedded in. Thus, it is the name you'll find in the code. (since there 
is no lexical entity class, we can fall back to the using class without having to 
explain to much). (And __TRAIT__ is also not changed on usage when composing 
traits, it is a lexical reference).

That's what comes to my mind when thinking about this question.

I would suggest that you write a mail to the internals mailing list to start a 
discussion. This might still be a controversial thing.

I will classify it as intended behavior for the moment

Thanks (and please start a discussion on the mailing list if you disagree)
 [2012-02-09 20:44 UTC]
-Status: Open +Status: Not a bug
 [2012-02-09 20:44 UTC]
Forgot to set the status.
 [2013-02-14 09:59 UTC] kusmierz at o2 dot pl
The real example of "Not a bug, it's feature!". How can i refere to original method name then?
 [2013-02-18 17:26 UTC] kusmierz at o2 dot pl
marc, your method doesn't work if you call the traited method from trait...
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 15 21:01:28 2024 UTC