Selective PHP autoload

I am writing a add-on module which is integrated with an existing PHP application; Because I am using a MVC pattern, and which may requires lot of inclusion of classes (which might not be used at all depending on the action of the user), I decide to use autoloading of classes.

However, I have to ensure that the autoload function does not interferes with the normal operations of the existing applications.

  1. Does autoload only kicks in if a class name is not defined?

  2. Say I have to write another module which uses its own autoload functions (say, I have an autoload for a module, since they each reside in their own folder), how do I differentiate which module is it for?

For #2, I thought of 2 options. Either prefix the class name with the module name (Such as 'MyNewModule_View_Default' and 'AnotherModule_View_Default'), or use file_exists to check the include file exists.

Other suggestions are welcomed too!


  1. Just check if the class that is to be loaded already exists with class_exists() before actually loading it in your autoloader implementation. Especially if you have multiple registered autoloaders (see 2).

  2. You can specify multiple autoloaders in a stack via spl_autoload_register(). The registered functions are executed in the order in which they where registered until the class is successfully loaded. You can specify different autoloaders for different modules for example. Or you can do a namespacing approach like in the Zend_Framework, if you have control over class names.

  1. Yes, autoloader is only called when class name is not found.

  2. Usually you'd check the class' namespace (pre 5.3 you use pseudo-namespaces, usually separated by an underscore). So your autoloader would only load classes that are under the namespace(s) of your application.

Need Your Help

Angular & Ionic, HTTP Get not working in real device IOS

angularjs ionic-framework ionic

I have problem in my app , when I run the app in local host it's working without problem and I can see the channel list but when I try to test the app by physical device it doesn't show anything. I...

C# Excel Interop - Suppress 'Publishing' dialog when invoking Worksheet.ExportAsFixedFormat

c# .net excel export interop

I am using Excel Interop to open an xlsx file and save that as a pdf document. Upon invoking the 'ExportAsFixedFileFormat' method a dialog titled "Publishing" is displayed to indicate the progress....