|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2012-11-27 18:52 UTC] spamfreedave-zend at yahoo dot com
 Description:
------------
I would like my trait functions to know what alias (if any) was used to call them.
The idea came from discovering that you can give a trait function multiple aliases (see Test Script).
It occurs to me that you could have some interesting dynamic behavior if the trait function was able to determine what name (alias) was used to call it.
If possible, a new constant __ALIAS__ could be created to store this value.
Thank you for your consideration.
Test script:
---------------
<?php
trait TestTrait
{
	public function test() { print __FUNCTION__ . ', ' . __ALIAS__  . "\n"; }
}
class TestClass
{
	use TestTrait { test as test2; test as test1; }
}
$c = new TestClass();
$c->test1();
$c->test2();
Expected result:
----------------
test, test1
test, test2
Actual result:
--------------
test, __ALIAS__
test, __ALIAS__
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 21:00:02 2025 UTC | 
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; } }