任何人都知道用于 Trait 在类中重新定义名称的 PHP 魔术常量?

编程入门 行业动态 更新时间:2024-10-27 10:26:57
本文介绍了任何人都知道用于 Trait 在类中重新定义名称的 PHP 魔术常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为了简单起见,我注意到 PHP 似乎没有提供任何魔法常量来确定某个 trait 在类中更改为什么名称.由于这听起来让我感到困惑,我将举一个例子,因为它相当简单,并且预计它会出现在新的 PHP 5.5 中,我看不出有什么方法可以做到.所以这里是:

To make it simple, I have noticed that PHP doesn't seem to offer any magic constant for determining what the name that a trait has been changed to in a class. Since this sounds confusing to me in words, I will give an example, as it is rather easy and would expect it to be somewhere in the new PHP 5.5, I don't see a way to doing it. So here it is:

假设我们有一个类,它使用了一些与类中的一些函数冲突的特性,例如:

Say we have some class, that uses some trait that conflicts with some function inside the class, example:

class SomeClass { use \Name\Space\SomeTrait { SomeFunction as private NewFunctionName; } function SomeFunction() { $this->NewFunctionName(); } }

因为,显然这个类有SomeFunction"函数,我们知道在我们包含的 SomeTrait 中,有一个函数与我们在这个类中的函数名匹配.现在,由于SomeFunction"通过\Name\Space 内的特征进入这个类,这两个函数做了两件不同的事情,但碰巧使用了相同的名称,并且在另一个函数或字面上的SomeFunction"中,然后我们使用特征中的SomeFunction",通过NewFunctionName"调用它.

Since, obviously this class has the "SomeFunction" function, and we are aware that inside of SomeTrait we have included, is a function that is matching by name to a function we have inside this class. Now, since the "SomeFunction" came into this class via a trait inside of a \Name\Space, these 2 functions do 2 different things, but happen to use the same name, and inside of either another function or literally our 'SomeFunction', we then use the "SomeFunction" from our trait, by calling it by the "NewFunctionName".

所以希望我没有在这里失去任何人,因为这就是我在上述情况下的问题.在\Name\Space\SomeTrait\SomeFunction() 中,如何获得该特征函数也被分配的NewFunctionName"?人们会想到使用一种神奇的方法,例如__FUNCTION__,或__METHOD__,甚至__TRAIT__,除了这些都没有给出预期的结果,所以有没有人知道一种方法来获取此信息而不将其作为参数传递给函数并导致黑客代码?也许 PHP 5.6 需要添加一个新的 Magic Constant __AS__,或者调整 __TRAIT__ 的结果,我不明白为什么 __TRAIT__ 和 __FUNCTION_ 需要返回相同的信息(或几乎相同的信息).任何帮助都会很棒,如果它是一种非常规的hacky方法,我有兴趣查看我的选项,直到我可以用php打开关于此的错误报告.(如果真的是bug)

So hopefully I haven't lost anyone here, as here is what my question comes down to in the above scenario. Within the \Name\Space\SomeTrait\SomeFunction(), how could one get the "NewFunctionName" that this trait function was assigned too? One would think to use a magic method, such as __FUNCTION__, or __METHOD__, or even __TRAIT__, except none of these give the expected result, so does anyone know a way to get this information without passing it to the function as a parameter and resulting in a hacky code? Maybe PHP 5.6 needs to add a new Magic Constant __AS__, or adjust the result of __TRAIT__, I dont understand why __TRAIT__ and __FUNCTION_ need to return the same information (or nearly the same information). Any help would be awesome, if it is an unconventional hacky method, Im interested in seeing my options, until I can open up a bug report with php regarding this. (if it is truly a bug)

我目前最简单的方法似乎是,

My current, least hacky, method seems to be,

debug_backtrace()[0]['function']

虽然它有效,但我觉得它做了很多事情来获得一个简单的字符串,特别是如果你经常使用这个函数.:/

and though it works, I feel it is doing a lot to get a simple string, especially if you use the function a lot. :/

推荐答案

您当前的解决方案将是最好的,只需进行一些调整:

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

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

DEBUG_BACKTRACE_IGNORE_ARGS 和 1 的帧数限制将使回溯更浅、更快、占用更少的内存.

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

更多推荐

任何人都知道用于 Trait 在类中重新定义名称的 PHP 魔术常量?

本文发布于:2023-05-29 21:35:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/353346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   魔术   类中   定义   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!