我有很多类中使用的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?
答案 0 :(得分:0)
在黑暗中拍摄,但因为你还没有张贴你尝试过的东西......
我认为你要找的是_METHOD__
在您的火车功能中,您可以调用 METHOD 来获取当前别名。
if (__METHOD__ == "testFunction") { //has not been renamed