Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there performance downsides while using autoloading classes in PHP?

Currently I load in all my classes by including a "all.inc.php" file on every page of my site, this file then goes on to include all the config, classes, functions, etc. that I will use on the whole site.

My issue with this is that often I use classes that only pertain to certain pages/sections of a website, so often I am including a bunch of classes at the start of the page which will not be used.

Obviously autoloading the classes would fix this issue, so my question is, would autoloading the classes give me a performace downside as the server then has to check if a file exists? And if there is a downside, then is this downside worse than having to include a number of classes that may not get used on the page? Or is the difference negatable?

like image 903
Nick Avatar asked Nov 23 '11 10:11

Nick


2 Answers

This article has some information and benchmarks: PHP autoload performance (archived). Conclusion:

Autoloading does not significantly degrade performance. Include_path lookup, reading and parsing PHP scripts from disk takes much longer time than that bare autoloading logic costs.

like image 104
Gfox Avatar answered Oct 16 '22 14:10

Gfox


Autoloading a class is almost as fast as including the class in the normal way. Switching to autoloading will improve performance in your case, because PHP loads less files and classes.

like image 43
Sjoerd Avatar answered Oct 16 '22 13:10

Sjoerd