Rolling and packaging PHP scripts

I was just reading this thread where the pros and cons of using include_once

and were discussed require_once

. From this discussion (especially Ambush Commander's answer ), I removed the fact (?) That any kind of inclusion in PHP is inherently costly as it requires a processor to parse a new file into OP codes, etc.

It got me thinking.

I wrote a small script that will "flip" multiple Javascript files into one (adding all the content to another file) so that it can be packaged to reduce HTTP requests and overall bandwidth usage.

Typically for my PHP applications, I have one includes.php file that is included per page and then includes all the classes and other libraries I need. (I know this is probably not the best practice, but it works - the __autoload

PHP5 feature does it better anyway).

Should I apply the same rolling techniques in my PHP files?

I know this suggests that premature optimization is evil, but let's take this question as a theoretical one, okay?

+1


source to share


4 answers


It would depend on whether it was more work to analyze several small files or analyze one large one. If you need files as needed (without saying that you absolutely have to do something like that), then presumably there would be significantly less compilation for some execution paths than if all your code was folded into one big PHP the file that the parser had to encode everything whether it's needed or not.



As per this question, this is out loud more than experience with the internals of the PHP runtime - it doesn't sound like there is any real benefit to doing it too much at all. If you are experiencing a serious slowdown in your PHP, I would be very surprised if using require_once turns out to be a bottleneck.

+1


source


There is a problem with Apache / PHP on Windows that causes the application to be extremely slow when loading or even touching too many files (a page that loads about 50-100 files can take a few seconds just for the file business). This issue occurs with both include / requirement and files (fopen, file_get_contents, etc.). Therefore, if you (or most likely someone else, due to the age of this post) ever run your application on apache / windows, you will need to reduce the number of downloaded files. Combine more PHP classes into one file (automated script because that would be helpful, I haven't found one yet) or be careful not to touch unnecessary file in your application.



+4


source


As you said: "premature optimization ...". Again, if you're worried about performance, use an opcode cache like APC, which makes this problem almost disappear.

+1


source


This is not an answer to your direct question, just about your "js packaging".

If you leave only the javascript files and include them separately in your HTML source, the browser will cache those files. Then on subsequent requests, when the browser asks for the same javascript file, your server will return 304 unchanged header and the browser will use the cached version. However, if your "wrapping" javascript files together on every request, the browser will reload the file on every page load.

0


source







All Articles