Load a class with a different name than the one passed to the autoloader as an argument

Basically, I have the following problem, I want to use the functions of the new PHP namespace. Unfortunately I am running PHP version (5.3.2) where namespace support for autoload for linux still seems to be buggy and does not work (PHP should be able to automatically load the class file by its namespace without the need for a custom autoloader. but it doesn't work).

What I want to achieve is write an autoloader that I can just remove once the php namespace functions are working correctly (there seems to be a speed advantage when you are not using a custom autoloader) so you need to change as less code , and possibly later.

So I have a call like this:

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

      

which is correctly passed to the "system \ libraries \ file \ XML" string in my autoload. I can download the appropriate file "system / libraries / file / XML.class.php". However, the class there will be called

class XML { ... }

      

(or something other than "system \ libraries \ file \ XML") and therefore have a different name than the one PHP will try to load it with. So, is there an easy way to load this class ("XML") that has a different name than the name I pass to the autoloader function? Can I possibly do something in the autoloader to achieve this behavior? (I am using spl_autoload_register).

I know that even if it works, I still won't be able to use the full power of the namespace, since the (simple) autoloader won't respect the "use namespace" directive and I'll still have to use rather long names to load the class. However, if I understood PHP namespaces correctly, I could leave the code as if I later switched to using native namespace support instead of my autoloader.

If what I'm trying to do doesn't make any sense in your opinion, or if I misunderstood the namespaces, tell me (I haven't used PHP's namespace capabilities yet).

+1


source to share


1 answer


I would upload a file (which creates the class XML

) and then the class alias XML

matches the system\libraries\file\XML

named class :

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

      

More generally:



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

      

Although I'm not entirely sure what there class_alias

might be an alias for the class names ...

+3


source







All Articles