This blog talks about the life span of a PHP script i.e. the internal execution of a PHP script. Before working on PHP, I worked on Java and there I learned about the life cycle of Servlet and Struts. So one day, thought arised in my mind regarding the internal execution of a PHP script. Its good know in depth about the language on which we works, this is what I feel. I explored, read about Zend Module structure, PHP internals, and extension and finally I came to know about the life span of PHP. So here goes the Life cycle of PHP.
Brief Overview :
- We never start any PHP daemon or anything to run PHP script. When we start Apache, it starts the PHP interpreter along itself
- PHP is linked to Apache (SAPI i.e. a Server API) using mod_php5.so module
- PHP as a complete consists of 3 modules (PHP Core, Zend Engine and Extensions)
- PHP Core : Handles the requests, file streams, error handling and other such operations
- Zend Engine(ZE) : Converts human readable code into machine understandable tokens(op-codes). Then it executes this generate code into a Virtual Machine
- Extensions : Bunch of functions, classes, streams made available to the PHP scripts, which can be used to perform certain tasks. For example, we need mysql extension (php_mysql) to connect to MySQL database using PHP
- While Zend Engine executes the generated code, the script might require access to a few extensions. Then ZE passes the control to the extension module/layer which transfer back the control to ZE after completion of tasks
- Finally Zend Engine returns back the result to PHP Core, which gives that to SAPI layer, and finally which displays it on your browser
Life Span of PHP in depth:
Above was brief overview on PHP life span. Now let us understand life span of PHP script in depth.
As I wrote that when we start Apache Server, it also starts PHP interpreter along itself . Now the PHP start up takes in 2 steps:
As I wrote that when we start Apache Server, it also starts PHP interpreter along itself . Now the PHP start up takes in 2 steps:
- 1st step is to perform initial setup of structures and values that persists for the life of SAPI
- 2nd step is for transient settings that only last for a single page request
First Step of PHP start up:
This step takes place even before any page request is being made.
- As we start Apache, it starts PHP interpreter
- PHP calls MINIT method of each extension, which is being enabled. In php.ini file we can see the modules which are being enabled by default. MINIT refers to Module Initialization. Each Module Initialization method initializes and define a set of functions, classes which will be used by future page requests
PHP_MINIT_FUNCTION(extension_name) {
/* Initialize functions, classes etc */
}
}
Second Step of PHP start up:
- When a page request is being made, SAPI layer gives control to PHP layer. PHP then set up an environment to execute the PHP page requested. In turn it also create a symbol table which stores various variables being used while executing this page
- PHP then calls the RINIT method of each module. RINIT refers to Request Initialization Module. Example of RINIT module implementation is the Session’s module. If enabled in php.ini, the RINIT method of Sessions module will pre-populate the $_SESSION variable and save in the symbol table
PHP_RINIT_FUNCTION(extension_name) {
/* Initialize session variables, pre-populate variables, redefine global variables etc */
}
Like PHP start up, shut down process also happens in 2 steps.
- After the page execution is complete, PHP starts the cleanup process. In turn it calls RSHUTDOWN method of every extension.
- RSHUTDOWN method, destroys the symbol table (memory management) by calling unset( ) on all variables maintained in the symbol table
PHP_RSHUTDOWN_FUNCTION(extension_name) {
/* Do memory management, unset all variables used in the last PHP call etc */
}
Second Step of PHP shut down:
- PHP calls the MSHUTDOWN method of every extension, which is basically the last chance for every extension to unregister handlers and free any persistent memory allocated during the MINIT cycle
PHP_MSHUTDOWN_FUNCTION(extension_name) {
/* Free handlers and persistent memory etc */
}
No comments:
Post a Comment