Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to include a lot of files in PHP like it is for file based Sessions?

After reading about how file based PHP sessions are not the greatest for performance, it has me thinking. Does this mean a PHP script including a lot of files is bad as well? Since it is including a file or is this different from the way session data files are retrieved?

like image 312
JasonDavis Avatar asked Jan 17 '10 23:01

JasonDavis


2 Answers

You should use spl_autoload_register() and OOP. This way, no matter how small your project currently is or how big it will evolve over time (and it would be dumb to exclude this possibility), PHP will only include what it needs, no more, no less.

That's the perfect future-oriented balance between runtime RAM usage, the maintainability of the code and the effects of hard disk latency time, I'd say, provided you're modularizing your code properly, of course (and XDebug helps here).

Having said that, it implies the badness of including unused files.

Inclusion of files, no matter which way (spl_autoload_register() or otherwise), should be done with absolute paths, due to the php.ini directive include_path, which PHP would search through for your files when using relative paths.

And a small extra-note to why "include 'foo.php'" works like "include './foo.php'" (the "normal" way of including files): it's because the directory "." is part of include_path by default.

like image 73
Flavius Avatar answered Oct 05 '22 05:10

Flavius


I think everyone who fiddles with PHP comes to the point when their libraries become large, and worries come up for the performance.

My experience is that yes, if you always load all your libraries, then this is going to eat up precious memory (Of which you are allocated only a fixed number of megabytes per process). I have had source code files weighing 300-400kb (with comments) that ate 2-3 MB per script instance. Seeing that a script gets only 16-32 MB with many shared hosts, that is a lot. Also, the processing of such huge files often comes in at up to half a second per request, which is way too much.

So, splitting up is definitely necessary, and easy to do with Autoload and consorts. Check out my answer to this question for a few suggestions on how to split up your code wisely. There is also a link to a question about how to organize a large PHP project, which yielded great results. I'm in the process of figuring out the perfect structure myself, and not done yet. :)

like image 45
Pekka Avatar answered Oct 05 '22 05:10

Pekka