Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would performance suffer using autoload in php and searching for the class file?

I've always struggled with how to best include classes into my php code. Pathing is usually an issue but a few minutes ago i found this question which dramatically helps that. Now I'm reading about __autoload and thinking that it could make the process of developing my applications much easier. The problem is i like to maintain folder structure to separate areas of functionality as opposed to throwing everything into a general /lib folder. So if i override autoload to do a deep search of a class folder including all subfolders, what performance hits can i expect?

Obviously this will depend on scale, depth of the folder structure and number of classes but generally I'm asking on a medium scale project will it cause problems.

like image 261
JoshReedSchramm Avatar asked Sep 25 '08 19:09

JoshReedSchramm


2 Answers

__autoload is great, but the cost of stating all the files in a recursive search function is expensive. You might want to look at building a tree of files to use for autoloading. In my framework, I consistently name files for their classes and use a map that is cached for the data.

Check out http://trac.framewerk.org/cgi-bin/trac.fcgi/browser/trunk/index.php [dead link] starting at line 68 for an idea of how this can be done.

Edit: And to more directly answer your question, without caching, you can expect a performance hit on a site with medium to heavy traffic.

like image 143
Gavin M. Roy Avatar answered Oct 23 '22 04:10

Gavin M. Roy


A common pattern (Pear, Zend Framework as examples...) is to make the classname reflect the path, so Db_Adapter_Mysql will be in at /Db/Adapter/Mysql.php, from somewhere that's added to the include-path.

like image 27
Greg Avatar answered Oct 23 '22 05:10

Greg