将类方法别名为全局函数
是否可以将类方法别名为全局函数?
我有一个类充当 PHP 中 gettext 函数的包装器。我有一个名为 _t()
的函数来处理翻译。
我希望能够从代码中的任何位置将其称为 _t()
,而不必通过实例化对象 ($translate::_t()
) 来执行此操作因为这看起来很笨拙。
我考虑过使用命名空间为对象提供别名:
Use translations\TranslationClass as T
尽管这是一个更好的改进:T::_t()
。它仍然不像只需要执行 _t()
那样需要。
是否有办法在整个应用程序中将 T::_t()
别名为 _t()
?
Is it possible to alias a class method as a global function?
I have a class that acts as a wrapper around the gettext functions in PHP. I have a function called _t()
that deals with translations.
I would like to be able to call it as _t()
from anywhere in my code without having to do it via an instantiated object ($translate::_t()
) as that seems quite unwieldy.
I thought about using namespaces to give the object an alias:
Use translations\TranslationClass as T
Although this is a better improvement: T::_t()
. it is still not as need as just having to do _t()
.
Are there anyways to alias T::_t()
as just _t()
across the whole application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建包装函数或使用 create_function()。
或者您可以动态创建一个函数:
You can either create a wrapper function or use create_function().
Or you can create a function on the fly:
您可以使用 eval() 函数将方法转换为全局函数。
此示例适用于静态方法。
You can use eval() function to convert methods as global functions.
This example works well with static methods.