2

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).

hakre
  • 193,403
  • 52
  • 435
  • 836
Michael Helwig
  • 225
  • 4
  • 10
  • What did you try this far in your autoloader? I think it should work even in that version of php. By the way, since you mention Linux you must note that all filenames are case sensitive. – chelmertz May 28 '11 at 13:44
  • Thanks, I know that in linux paths are ci, but this is not the problem, as far as I see. The autoloader just examines the string which it gets passed and loads the corresponding file via require_once, so it does not do anything besides the usual stuff. – Michael Helwig May 28 '11 at 14:04

1 Answers1

3

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...

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • It can, but currently I don't know, if it (maybe) require full-qualified names (starting with `\\`). As far as I remember classnames in strings are always treated as full-qualified. Thus your example should work. – KingCrunch May 28 '11 at 13:54
  • I will try that later. Looks promising. I didn't know about the class_alias function so that was definitely helpful. – Michael Helwig May 28 '11 at 14:05
  • Works.Thanks. However, still one thing that concerns me about my attempt at all: Strictly speaking, "system\libraries\file\XML" is not a valid variable name, so I guess it is also not a valid class name. I'm not sure if that could cause trouble somewhere... ? – Michael Helwig May 28 '11 at 18:05