Req #63629 [NoF]: New constant __ALIAS__ for trait functions to know how they were called
| From: | gron@php.net | Date: | Thu, 06 Feb 2014 14:46:41 +0000 |
| Subject: | Req #63629 [NoF]: New constant __ALIAS__ for trait functions to know how they were called | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-184184@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63629&edit=1
ID: 63629
Updated by: gron@php.net
Reported by: spamfreedave-zend at yahoo dot com
Summary: New constant __ALIAS__ for trait functions to know
how they were called
Status: No Feedback
Type: Feature/Change Request
Package: *General Issues
Operating System: Mac OSX
PHP Version: 5.4.9
Block user comment: N
Private report: N
New Comment:
Sorry, the last comment was with regard to the original question.
Looking at the example code of 'opitz at pluspol-interactive dot de', it looks to me code
generation, or templates would be much more useful here. I would not want to advocate for such usage
by introducing another magic constant.
However, I also can see a point in having a magic constant that allows to define unique names for
properties within a trait. But, that might be something else entirely.
Previous Comments:
------------------------------------------------------------------------
[2014-02-06 14:41:21] gron@php.net
Perhaps a naive question, but if you want a function to behave differently based on the name it has,
why don't you just provide distinct functions then?
------------------------------------------------------------------------
[2014-02-06 14:17:57] opitz at pluspol-interactive dot de
Following Scenario:
trait TestTrait
{
protected $var = null;
public function setMethod($value) {
$this->var = $value;
}
public function getMethod() {
return $this->var;
}
}
class TestClass {
use TestTrait {
setMethod as setMethod1;
setMethod as setMethod2;
getMethod as getMethod1;
getMethod as getMethod2;
}
}
$class = new TestClass();
$class->setMethod1('A');
$class->setMethod2('B');
echo $class->getMethod1();
echo $class->getMethod2();
This will output "BB" instead of "AB" With __ALIAS__ we could use different
poperties per Trait usage in the getter/setter.
A better aproch would be usage of templates, so the setter assignment can depend on trait inlusion:
Like
trait TestTrait
{
protected $<MyTraitVar> = null;
public function setMethod($value) {
$this-><MyTraitVar> = $value;
}
public function getMethod() {
return $this-><MyTraitVar>;
}
}
class TestClass {
use TestTrait as TestTrait1, TestTrait as TestTrait2 {
TestTrait1::<MyTraitVar> as MyVar1;
TestTrait2::<MyTraitVar> as MyVar2;
TestTrait1::setMethod as setMethod1;
TestTrait2::setMethod as setMethod2;
TestTrait1::getMethod as getMethod1;
TestTrait2::getMethod as getMethod2;
}
}
------------------------------------------------------------------------
[2013-02-18 00:36:07] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
------------------------------------------------------------------------
[2012-11-28 09:55:11] gron@php.net
What about:
$backtrace=debug_backtrace();
echo $backtrace[0]['function'];
That seems to be also the common solution for getting the name of the calling
function.
------------------------------------------------------------------------
[2012-11-28 09:50:56] laruence@php.net
could you explain a little more: what i " some interesting dynamic behavior" ?
I mean , the real use case.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=63629
--
Edit this bug report at https://bugs.php.net/bug.php?id=63629&edit=1