1

Lets say I'm running a PHP app like Magento, PrestaShop.

Now these system have their own complex autoloaders to load controllers based on routes, required classes, models, etc.

Most of the time as a developer I create module to implement some sort of custom functionality. These modules reside in /modules/mymodule/. Some of these modules have their own classes like models, tools, etc.

I want to know if it's good practice to use autoload.php in modules (they opposite being including files manually)? What if there are 20+ modules and all of them have registered an autoloader? Any performance/architecture issues?

The autloader function is a simple one:

spl_autoload_register(function ($class) {
    include('classes/'.$class.'.php');
});

Namespaces are not allowed as of yet (PHP 5.2 support). Class names are usually namespaced.

4

Okay, first things first: you're running PHP 5.2!? There's not really much reason to stay with that other than having out of date servers, and that means it's either time to switch hosts or update your servers because PHP 5.2 has some major known security holes which aren't ever going to be fixed. This'll also give you access to a bunch of new features, the most relevant to your situation being namespaces, which will actually come up later.

The current best/most common practice for autoloading is PSR-0/PSR-4 which establish clear, consistent conventions for autoloading. This is mostly thanks to the fact that Composer has become such a widely-used part of the PHP toolchain. The main advantage of using consistent autoloading where classnames/namespaces are tied to the file system is that you don't need to have multiple autoloaders registered, you only need one which can do most of the lookup work for you.

So in the case of something using PSR-4, then a class like Namespace\Namespace2\Classname (or Namespace__Namespace2__Classname in the pre-namespace world) maps to a with a path like Namespace/Namespace2/Classname.php, which the autoloader can easily programmatically include for you once. From a performance perspective, it's faster than using require_once*, and from a coding perspective, it means you don't have to remember to add your include/require statements in every file. What's more, you don't have to keep those include statements up to date when you want to rename or move a class--the autoloader takes care of linking everything up for you.

*I should concede that opcode caching makes the speed argument largely irrelevant for the most part

  • Thank you for the answer. I am all aware of later PHP version advantages, Composer and PSR. The reason why we are forced to keep PHP 5.2 is because the system officialy supports (like PrestaShop is a 3rd party system) and we have to develop addons and customizations to it's standards. The supported PHP version will be upgraded to PHP 5.4 next year. My question was more about having autoloaders that come from modules. If I could, I'd have all classes in one place, but a module must keep the classes it needs within itself (image a Wordpress plugin). That's what I wanted to know more about. – gskema Aug 11 '15 at 5:48
  • Then I'd still say try and maintain as consistent a naming and organization convention as possible so that you only need one autoloader algorithm in play; otherwise that puts a lot of additional demand on PHP because it'll need to cycle through all your registered autoload functions before it can actually know it should throw a class not found error. – moberemk Aug 11 '15 at 13:56
  • Plus, organizing per-module autoloaders will get messy fast--you have to either include or require each autoload registration file as part of your module enabling process, which I'd argue becomes more of a developer headache than anything else. – moberemk Aug 11 '15 at 13:57
  • That's the thing - I can't edit the system code (like WordPress code). I must come with everything ready to go inside my module. – gskema Aug 11 '15 at 14:04
  • In that case the next question becomes how will your module be loaded? If you've got one known file that will be loaded when the module is enabled then I suppose that yes registering your own autoloader is the best solution you have access to in this environment with these restrictions; the alternatives are arguably worse, like concatenating all your code into a single file. – moberemk Aug 11 '15 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.