Anyone know of a PHP Magic Constant for Trait's Redefined Name in a Class?

蹲街弑〆低调 提交于 2019-12-06 03:40:35

This is the solution I ended up using. It's not very good but probably better than using the debug_backtrace function.

Problem example:

Trait ExampleTrait {
    protected function doSomethingRecursive() {
        // this is a problem because it could be renamed
        $this->doSomethingRecursive(); 
    }
}

Solution example:

Trait ExampleTrait {

    protected function doSomethingRecursive() {
        // this is a problem because it could be renamed
        $this->__internal_doSomethingRecursive(); 
    }

    private function __internal_doSomethingRecursive() {
        // this works because the class would have 
        // used and renamed the above function 
        // but this "internal" function *should*
        // remain available under it's original name
        $this->__internal_doSomethingRecursive(); 
    }

}

Of course it's possible to break this but for most cases it should be fine. You could also include the name of the trait in the internal function name to further prevent conflicts.

Your current solution is going to be the best, with a couple tweaks:

debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,1)[0]['function']

The DEBUG_BACKTRACE_IGNORE_ARGS and the frame limit of 1 there will make the backtrace shallow, faster, and use less memory.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!