some background to the questions
I have just been browsing through some *.php files used in the moodle CMS. Moodle uses PHP-Scripts to generate content of HTML-pages send to the visitors dynamically. Often something like this happens ("inclusion cascade"):
// file: file1.php
require(file2.php);
and
// file: file2.php require(file3.php); require(file4.php);
etc...
Indeed starting from the requested *.php file until finally producing some output there is quite a cascade of inclusions of other files necessary. Even if this makes much sense it worries me for the reason of its impact on speed/performance. It seems that each time a lot of initialization is redone.
The question
Knowing that the HTTP protocol a stateless protocol, it would appear to me that for each request that is sent to the server, it is necessary to run through all possible initialisations done in the PHP/CGI code over and over again. Is this a valid/true assumption?
Example: I have a need to access a database and this I want to do safely using some objects that help with doing all this "safer" prepare statements/sanitizing etc. The object used for this is hence created in a file I include ( i.e. myDatabaseAccessObject.php
).
With regard to the example the question is:
whether it is true, that due to the nature of HTTP being stateless, that there is no chance to keep the work of setting up (i.e parsing) the myDatabaseAccessObject.php
from being done all over again upon each request?
Or does PHP over a way to cache the work already done? (if so, is done in a transparent way (i.e. the script-author can tell what to cache) or obscured way, the php-engine does some caching not visible do the author?)
Is it that I have a absolutely flawed perception of what is going on, or is indeed work done over and over again, which could be saved if some initialization necessary for the PHP-Script would have been saved between multiple subsequent requests?
You are completely right. All database connections en other initializations are done on every PHP script execution. It is exactly because of the statelessness of the HTTP protocol.
That being said, there are ways to speed up the process. There is the PHP session handling that can do stuff for you (although it can't cache connections), Smarty for example has a decent caching and compiling system, etc.