PHP全局命名空间别名 [英] PHP Global namespace aliases

查看:86
本文介绍了PHP全局命名空间别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景.

我正在项目中实现名称空间.

I am implementing namespaces into my projects.

我有自己的自定义桥库,该桥库可以调用Zend等其他库来进行繁重的工作.

I have my own custom bridge library that calls other libraries like Zend to do the heavy lifting.

我在自定义桥库中使用完全限定的名称空间没有问题,但是希望在我的控制器,模型和视图中使代码尽可能简洁.

I have no problem using fully qualified namespaces in my custom bridge library but would like to keep the code as terse as possible in my controllers, models and view.

以下是我要使用的一些别名的示例:

Here is an example of some aliasses i would like to use:

use BridgeLibName\Stdlib\Arrays as arr;
use BridgeLibName\Stdlib\Objects as obj;
use BridgeLibName\Stdlib\Strings as str;
use BridgeLibName\Stdlib\Numbers as num;
use BridgeLibName\Stdlib\File as file;
etc.........

示例用法:

$file = new file('path/to/file.txt');
$file->create();

$obj = arr::toObject(['key1'=>'value1']);

是否有可能以任何方式创建可全局访问且不会在每个文件末尾丢弃的别名或常量?

is it possible in any way to create an alias or constant that can be globally accessible and not discarded at the end of each file?

某种可以使这些别名保持不变的引导程序文件.

Some kind of bootstrap file that can make these aliases stick.

推荐答案

在我写这个问题时,我想到了一个解决方案.

As I was writing the question i thought of a solution.

您可以通过创建扩展命名空间类的类来伪造它.

You can fake it by creating classes that extend the namespaced classes.

示例:

class arr extends BridgeLibName\Stdlib\Arrays{

}

要记住的一件事:

如果要扩展类,则必须加载命名空间类.

If you are going to extend the classes the namespaced class will have to be loaded.

如果使用过多,这可能会对性能产生影响,因为别名和名称空间仅根据需要加载.

This could have performance implications if used too much since aliases and namespaces are only loaded as needed.

由于我仅使用它来桥接到其他类,所以桥接文件中的逻辑很少.

Since I am only using it to bridge to other classes there is very little logic inside my bridge files.

这些桥文件依次正确使用别名和名称空间,从而根据需要加载实际文件.

These bridge files in turn uses aliases and namespaces correctly thus loading the real files as needed.

如果您对实现不太谨慎,则可能会加载很多不必要的内容,并导致您的应用变慢且变得肿.

I you are not careful with the implementation you can load a lot of unnecessary stuff and cause your app to become slow and bloated.

我注意到的一件好事是,像netbeans这样的优秀IDE似乎也可以使用此方法来自动完成.

A nice thing I noticed is that good IDEs like netbeans also seems to be able to do auto completion with this method.

如果有更好的方法可以做到这一点,请告诉我.

If there is a better way to do this please let me know.

只需考虑对该方法进行修正即可使用不必要的类实例化解决该问题.

Just thought of an amendment to this method to fix the problem with unnecessary class instantiation.

核心库可以与普通的psr-0加载程序一起使用.

The core library can work with the normal psr-0 loader.

要让别名自动加载,我在命名空间类旁边创建了一个名为include的附加目录.

To have the aliases autoload I created an aditional dir named includes next to my namespaced class.

在作曲家中您是这样描述的:

in composer you describe it like so:

"autoload": {
    "psr-0": {
        "BridgeLibName\\": "."
    },
    "classmap": ["include/"]
}

现在,您的库将按预期从正确的名称空间加载,并且您的别名类将根据需要自动加载.

Now your libraries will load as expected from the correct namespace and your alias classes will autoload as needed.

包含在include目录中的类现在可以扩展命名空间的类(如上所示),并且在使用前将不再加载.

Classes put into the include dir can now extend the namespaced classes ( as shown above ) and will no longer be loaded prior to being used.

现在,您有了全局别名,而不必通过加载未使用的类来牺牲性能.

Now you have global aliases without having to sacrifice performance by loading unused classes.

这篇关于PHP全局命名空间别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆