PHP spl_autoload_register PHP spl_autoload_register php php

PHP spl_autoload_register


Sure, looks good. The only thing you might do is register them in the order they're most likely to hit. For example, if your most commonly used classes are in services, then vos, then printers, the order you have is perfect. This is because they're queued and called in-order, so you'll achieve slightly better performance by doing this.


You could use:

set_include_path(implode(PATH_SEPARATOR, array(get_include_path(), './services', './vos', './printers')));spl_autoload_register();

Using spl_autoload_register without arguments will register spl_autoload which will look for the class name in the directories of the include path. Note that this will lowercase the class name before looking for it on the filesystem.


It's okay, but if these are just folders below a certain folder, e.g.

/library    /JonoB        /services        /vos        /printers

you might want to consider adding these to your classnames, e.g.

JonoB_Services_Foo, JonoB_Vos_Bar, JonoB_Printers_Baz

and then split the $classname by the underscore and take each part as folder name. This is similar to PEAR class name convention. This way you would only have one loader.

Instead of PEAR convention style classnames, you can also use namespaces (autoload example), but be aware that these require PHP5.3 which is not widely available on shared hosting yet. And your application won't be backwards compatible with PHP<5.3 then (if that's an issue).