php.net |  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
 [2012-02-09 20:23 UTC] marc at easen dot co dot uk
Description:
------------
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:
---------------
<?php
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;
$instance->foo();
echo PHP_EOL;
echo 'bar()' . PHP_EOL;
$instance->bar();


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

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


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

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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-09 20:43 UTC] gron@php.net
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)
Stefan
 [2012-02-09 20:44 UTC] gron@php.net
-Status: Open +Status: Not a bug
 [2012-02-09 20:44 UTC] gron@php.net
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-2019 The PHP Group
All rights reserved.
Last updated: Mon May 20 09:01:26 2019 UTC