加载名称与传递给自动加载器作为参数的类不同的类 [英] Load a class with a different name than the one passed to the autoloader as argument

查看:114
本文介绍了加载名称与传递给自动加载器作为参数的类不同的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有以下问题:我想利用PHP的新名称空间功能.不幸的是,我正在运行一个PHP版本(5.3.2),其中对Linux的namespace-autoload-support似乎仍然有问题,并且无法正常工作(PHP应该能够通过其命名空间自动加载类文件,而不必使用自定义自动装带器,但这不起作用.

我要实现的是编写一个自动加载器,只要php的命名空间功能正常工作(不使用自定义自动加载器似乎有速度优势),我就可以删除该自动加载器,而只需更改尽可能少的代码即可.以后可能.

所以我有一个这样的电话:

$filereader = new system\libraries\file\XML();

正确地作为字符串"system \ libraries \ file \ XML"传递给了我的自动加载功能.我可以加载相应的文件"system/libraries/file/XML.class.php".但是,其中的类将被命名为

class XML { ... }

(或与"system \ libraries \ file \ XML"不同的名称),因此名称与PHP尝试加载它的名称不同.那么,是否有一种简单的方法来加载该类("XML"),该类的名称与我传递给自动加载器函数的名称不同?也许我可以在自动装带器中做一些事情来实现这一功能? (我正在使用spl_autoload_register).

我知道,即使解决了,我仍然无法使用命名空间的所有功能,因为(简单的)自动加载器不会遵守"use namespace"指令,并且对于正在加载课程.但是,如果我正确理解了PHP的命名空间功能,则可以在以后切换到使用本机命名空间支持而不是我的自动加载器时保留代码.

如果您认为我尝试执行的操作根本没有意义,或者如果我误解了名称空间,请告诉我(-我尚未使用PHP的名称空间功能).

解决方案

我将加载文件(创建XML类),然后将XML类别名为正确命名的system\libraries\file\XML类:

class_alias('XML', 'system\libraries\file\XML');

更一般地:

class_alias(basename($class), $class));

尽管我不太确定 class_alias 是否可以为命名空间类提供别名...

basically, I have the following problem: I want to make use of PHP's new namespace features. Unfortunately, I'm running a PHP version (5.3.2) in which namespace-autoload-support for linux still seems buggy and does not work (PHP should be able to load the class file automatically by its namespace without having to use a custom autoloader, but that doesn't work).

What I want to achieve is to write an autoloader that I can simply remove as soon as the php's namespace features work correctly (there seems to be a speed advantage when not using a custom autoloader) with having to change as less code as possible afterwards.

So I have a call like this:

$filereader = new system\libraries\file\XML();

which gets passed correctly as the string "system\libraries\file\XML" to my autoload-function. I can load the corresponding file "system/libraries/file/XML.class.php". However, the class in there will be named

class XML { ... }

(or something different than "system\libraries\file\XML") and so have a different name than the one by which PHP will try to load it. So is there an easy way to load that class ("XML") which has a different name than the name which I pass to the autoloader function? Can I perhaps do something in the autoloader to achieve that behaviour? (I'm using spl_autoload_register).

I know that even if it worked out I would still not be able to use all features of namespacing, since a (simple) autoloader would not respect the "use namespace" directive and I would still have to use rather long names for loading a class. However, if I understood PHP's namespace-features correctly, I could leave the code as it is when I later switch to using native namespace support instead of my autoloader.

If what I try to do does not make sense at all in your opinion or if I misunderstood namespaces, please tell me (- I have not used PHP's namespace features yet).

解决方案

I would load the file (which creates the XML class) and then alias the XML class to the properly namespaced system\libraries\file\XML class:

class_alias('XML', 'system\libraries\file\XML');

More generally:

class_alias(basename($class), $class));

Though I'm not quite sure whether class_alias can alias to namespaced classes...

这篇关于加载名称与传递给自动加载器作为参数的类不同的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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