如何知道函数何时被别名或重命名?
问题描述:
我有许多类使用PHP的特点。其中一些类选择重命名特征函数。特质中的其中一种方法想自称。它如何自称?如何找出重命名的内容?如何知道函数何时被别名或重命名?
<?php
Trait TestTrait
{
protected function testFunction()
{
// Calls TestClass function
// instead of the trait function.
$this->testFunction();
// Calls TestClass function
// instead of the trait function.
static::testFunction();
// Fatal error: Call to protected method
// TestTrait::testFunction() from context 'TestClass'
TestTrait::testFunction(); # __METHOD__
// This works but requires that
// the trait know that the function
// has been renamed. How can we
// determine if has been renamed?
$this->traitTestFunction();
}
}
class TestClass
{
use TestTrait
{
testFunction as traitTestFunction;
}
function testFunction()
{
$this->traitTestFunction();
}
}
$test = new TestClass();
$test->testFunction();
相关:Anyone know of a PHP Magic Constant for Trait's Redefined Name in a Class?
答
在黑暗中拍摄,但因为你还没有发布,你已经尝试了什么......
我想你在找什么is__METHOD__
在你的火车功能,你可以拨打方法得到当前的别名。
if (__METHOD__ == "testFunction") { //has not been renamed
我做了测试,但可能没有在我的代码注释中说清楚。它总是返回'TestTrait :: testFunction',所以没用。 – diolemo 2015-01-09 23:30:30