3 ms·
Why do NEW PHP projects insist on not using namespaces? Also, why on earth would your autoload class need to handle the case when a class is already defined? T
by stephenr 14y ago
Why do NEW PHP projects insist on not using namespaces?
Also, why on earth would your autoload class need to handle the case when a class is already defined? Thats the whole point of autoloading - to load a class file when the class is not defined.
Of course, if projects adopted namespaces and sensible file naming, they wouldn't need their own autoload classes at all - SPL handles it out of the box with between one and three lines of code:
set_include_path(get_include_path() . PATH_SEPARATOR . MY_INCLUDE_ROOT); // if your include path needs to be set specifically
spl_autoload_extensions('.class.php,.interface.php,' . spl_autoload_extensions()); // if you prefer class/interface files to have a suffix
spl_autoload_register('spl_autoload', false);
That's it. You need to lowercase your filenames, and namespaces translate to directories - both completely logical IMO.