How to make a variable private to a trait?

后端 未结 4 660
广开言路
广开言路 2021-02-05 13:17

I\'d like to reuse a functionality several times in a single class. This functionality relies on a private variable:

tr         


        
4条回答
  •  星月不相逢
    2021-02-05 13:34

    I confirmed that you can alias the same function multiple times, which was a surprise to me. Although ZendStudio appears to only 'code assist' on the last alias of the function.

    Being able to alias the same function multiple times could lend itself to some interesting behavior if the Trait function can determine what name it was called as. But it does not appear that we can determined the 'aliased' function within a trait function.

    Here's my test code:

    test1();
    $c->test2();
    

    Output:

    TestClass, TestTrait, TestTrait::test, test
    TestClass, TestTrait, TestTrait::test, test
    

    Perhaps it would be nice to add a new __ALIAS__ constant for trait functions to determine what alias they were called as.

    In fact I have created PHP feature request for this:

    https://bugs.php.net/bug.php?id=63629

提交回复
热议问题