views:

87

answers:

3

Hi,

I need some help understanding internal workings of PHP.

Remember, in old days, we used to write TSR (Terminate and stay resident) routines (Pre-windows era)? Once that program is executed, it will stay in memory and can be re-executed by some hot-key (alt- or ctrl- key combination).

I want to use similar concept in web server/applications. Say, I have common_functions.php which consists of common functions (like Generate_City_Combo(), or Check_Permission() or Generate_User_Permission_list() or like) to all the web applications running on that apache/php server.

In all the modules or applications php files, I can write:
require_once(common_functions.php) ;

which will include that common file in all the modules and applications and works fine.

My question is: How does php handle this internally?
Say I have: Two applications AppOne and AppTwo.
AppOne has two menu options AppOne_Menu_PQR and AppOne_Menu_XYZ
AppTwo has two menu options AppTwo_Menu_ABC and APPTwo_Menu_DEF

All of these four menu items call functions { like Generate_City_Combo(), or Check_Permission() or Generate_User_Permission_list() } from common_functions.php

Now consider following scenarios: A) User XXX logs in and clicks on AppOne_Menu_PQR from his personalized Dashboard then s/he follows through all the screens and instructions. This is a series of 8-10 page requests (screens) and it is interactive. After this is over, user XXX clicks on AppTwo_Menu_DEF from his personalized Dashboard and again like earlier s/he follows through all the screens and instructions (about 8-10 pages/screens). Then User XXX Logs off.

B) User XXX logs in and does whatever mentioned in scenario A. At the same time, user YYY also logs in (from some other client machine) and does similar things mentioned in scenario A.

For scenario A, it is same session. For Scenario B, there are two different sessions.

Assume that all the menu options call Generate_User_Permission_list() and Generate_Footer() or many menu options call Generate_City_Combo().

So how many times will PHP execute/include common_functions.php per page request? per session? or per PHP startup/shutdown? My understanding is common_functions.php will be executed once EVERY page request/cycle/load/screen, right? Basically once for each and every interaction.

Remember functions like Generate_City_Combo() or Generate_Footer() produces same output or does same thing irrespective of who or when is calling.

I would like to restrict this to once per Application startup and shutdown.

These are just examples. My actual problem is much more complex and involved. In my applications, I would like to call Application_Startup() routines just once which will create ideal environment (like all lookup and reference data structures, Read-Only-Data, Security-Matrix, Menu-options, context sensitive business execution logic etc..). After that all the requests coming to server need not spend any time or resources to create environment but can instantly refer "already-created-environment".

Is this something feasible in PHP? How? Could you point me to someplace or some books which explains internal working of PHP?

Thanks in advance.

+2  A: 

PHP processes each HTTP request in a completely separate frame of execution - there is no persistent process running to service them all. (Your webserver is running, but each time it loads a PHP page, a separate instance of the PHP interpreter is invoked.)

If the time it takes for your desired persistent areas to be generated is significant, you may wish to consider caching the output from those scripts on disk and loading the cached version first if it is available (and not out of date).

Amber
Thank you Dav. Is there a way to have one? Many a times, I have seen php applications creating object instances left-right-and-center with each and every http request. Being old C developer, I find it very in-efficient. Is there a way out?ThanksShailesh.
Shailesh Raval
It's inherent to the way PHP works - it's *technically* designed as a preprocessor (albeit it has somewhat advanced since its original creation). The PHP interpreter has no way to serve multiple requests simultaneously, because it's designed to be invoked by a separate server (as opposed to running on its own).
Amber
+1  A: 

PHP(in almost all cases) is page oriented. There is no Application_Startup() that will maintain a state across HTTP requests.

You can sometimes emulate this by loading/unloading serialized data from a database or $_SESSION, but there is overhead involved. Also, there are other cases where a memcached server can optimize this as well, but you typically can't use those with you typical virtual hosting services like cPanel.

If I had to build an app like you are talking about I would serialize the users choices into the session, and then save whatever needs to persist between sessions in a database.

There are several ORM modules for PHP like Doctrine which simplify object serialization to a database.

Sam Washburn
There is no equivalent of global.aspx or global.asa ?
Shailesh Raval
nope there isn't but you can create something like that with php: http://sarfraznawaz.wordpress.com/2009/09/08/application-variables-in-php/
Sarfraz
Sarfraz is correct, there is nothing like global.aspx/global.asa in php, and what he proposes in his link uses serialization and there is the unserialization overhead associated with that.It's something that PHP developers just have to deal with.
Sam Washburn
+1  A: 

I would say that you are likely prematurely optimizing, but there is hope.

You very frequently want multiple copies of your compiled code in memory since you want stability per request; you don't want separate requests operating in the same memory space and running the risk of race conditions or data corruption!

That said, there are numerous PHP Accelerators out there that will pre-compile PHP code, greatly speeding up include and require calls.

Randolpho
Is there a way, one can develop Application Server like TomCat?
Shailesh Raval