Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How PHP Works ?

little bit introduction about php start up.

  • Login to see the comments

How PHP Works ?

  1. 1. How PHP works ? Ravi Raj
  2. 2. About the presenter ?? <ul><li>Have 4+ years Experience in Web development </li></ul><ul><li>Have worked on few good products like , </li></ul><ul><li>currently working in @ infoedge india pvt. Ltd. </li></ul><ul><li>Know more about me </li></ul>
  3. 3. PHP LIFE CYCLE :) It's EASY ?? <ul><li>Parsing – Groups of tokens are collected into simple, meaningful expressions. </li></ul><ul><li>Compilation – Expressions are translated into instruction (opcodes) </li></ul><ul><li>Scanning – The human readable source code is turned into tokens. </li></ul><ul><li>Execution – Opcode stacks are processed (one opcode at a time) to perform the scripted tasks. </li></ul>
  4. 4. SOME FACTS -1 <ul><li>We never start any PHP daemon or anything by ourself. When we start Apache, it starts the PHP interpreter along itself... </li></ul>
  5. 5. SOME FACTS -2 <ul><li>PHP is linked to Apache (In general term SAPI i.e. a Server API) using module... </li></ul>
  6. 6. SOME FACTS -3 <ul><li>PHP as a whole consists of 3 modules (Core PHP, Zend Engine and Extension Layer) </li></ul>
  7. 7. SOME FACTS -4 <ul><li>Core PHP is the module which handles the requests, file streams, error handling and other such operations </li></ul>
  8. 8. SOME FACTS -5 <ul><li>Zend Engine(ZE) is the one which converts human readable code into machine understandable tokens/op-codes. Then it executes this generate code into a Virtual Machine. </li></ul>
  9. 9. SOME FACTS -6 <ul><li>Extensions are a bunch of functions, classes, streams made available to the PHP scripts, which can be used to perform certain tasks. For example, as we need mysql extension to connect to MySQL database using PHP </li></ul>
  10. 10. SOME FACTS -7 <ul><li>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. </li></ul>
  11. 11. Its Easy to understand !!! Right ?? <ul><li>OH!!! NO !!! … It's a overview only … Take a one more closer look now … </li></ul>
  12. 12. Let's START AGAIN :) -1 <ul><li>As we start Apache, it starts PHP interpreter </li></ul><ul><li>PHP calls MINIT method of each extension, which is being enabled. View your php.ini file to see the modules which are being enabled by default </li></ul><ul><li>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 </li></ul>
  13. 13. STEP -2 <ul><li>A typical MINIT method looks like: </li></ul><ul><li>PHP_MINIT_FUNCTION(extension_name) { </li></ul><ul><li>/* Initialize functions, classes etc */ </li></ul><ul><li>} </li></ul>
  14. 14. STEP -2 more ... <ul><li>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 will store various variables being used while executing this page. </li></ul><ul><li>PHP then calls the RINIT method of each module. RINIT refers to Request Initialization Module. Classic 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. </li></ul><ul><li>RINIT method can be thought as an auto_prepend_file directive, which is pre-appended to every PHP script before execution. </li></ul>
  15. 15. More on RINIT ... <ul><li>A typical RINIT method looks like: </li></ul><ul><li>PHP_RINIT_FUNCTION(extension_name) { </li></ul><ul><li>/* Initialize session variables, pre-populate variables, redefine global variables etc */ </li></ul><ul><li>} </li></ul>
  16. 16. PHP Shutdown ... <ul><li>After the page execution is complete either by reaching the end of the script or by call of any exit() or die() function, PHP starts the cleanup process. In turn it calls RSHUTDOWN method of every extension. RSHUTDOWN can be thought as auto_append_file directive to every PHP script, which no matter what happens, is always executed. </li></ul><ul><li>RSHUTDOWN method, destroys the symbols table (memory management) by calling unset() on all variables in the symbols table </li></ul>
  17. 17. More on shutdown ... <ul><li>A typical RSHUTDOWN method looks like: </li></ul><ul><li>PHP_RSHUTDOWN_FUNCTION(extension_name) { </li></ul><ul><li>/* Do memory management, unset all variables used in the last PHP call etc */ </li></ul><ul><li>} </li></ul>
  18. 18. More on shut down ... <ul><li>Finally when all requests has been made and SAPI is ready to shutdown, PHP call its 2nd step of shutdown process. </li></ul><ul><li>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. </li></ul><ul><li>And that brings us to the end of what we can call as PHP Lifecycle. Important point to note is that Step 1 of Startup and Step 2 of Shutdown happens when no request is being made to the web servers.8. Finally Zend Engine returns back the result to PHP Core, which gives that to SAPI layer, and finally which displays it on your browser. </li></ul>
  19. 19. It's Finish now ... <ul><li>Mail me if you have any query .. [email_address] </li></ul>