Sunday, January 29, 2017

Web Architecture with PHP

Web Architecture with PHP


Web Architecture Diagram of PHP Applications


Now lets put all these together and see how do they work actually. An Architecture Diagram is a logical diagram that shows how each of the components in a system is connected with each other and how are the data flowing between. If we draw architecture diagram for a typical PHP based web application it will be like the below diagram.

  • First user accessed the website through browser. That means user types the URL of the website in browser and hit go.
  • The page request on browser will reach to the Web Server (Apache).
  • Web server will collect that requested page (HTML or PHP or Image file etc) from its document root. (In our example it will be www folder in WAMP. You will see it in next one) 
  • Now if it is a static element like HTML , CSS , image file or Java Script file then Apache will send it directly to browser.
  • And Browser will render it to user on screen
  • If it is a PHP file then Apache sends the content of the file to PHP Interpreter. PHP interpreter interprets the PHP code and executes it. if DB operation is required it performs the same (E)
  • PHP Interpreter generates output (if the PHP code is to generate any output) and sends to Apache
  • Apache sends that content to browser
  • Browser renders it to users' screen
All static components like HTML, CSS files ,Image Files , Java Scripts etc doesn't need interpreter. Our web browsers are built to render them and display on screen properly. That is why if user requests for these kind of components Apache collects them from Document root and sends back to Browser directly.

Only if requested page is a PHP page Apache will send it to PHP interpreter to get it translated and executed.

That is why though those listed static components reside on Server we will consider them as part of User Interface and as they get rendered at user's browser we may refer them as Client side components. In web technology Browsers are Client terminals.

And for similar reason we will refer PHP files as Server side components as they have dependencies on another Server Side component PHP Interpreter and cannot be executed only on browsers.

Now let me explain the keywords of the definition as you will be able to understand them better
1. PHP files are kept on Server (in Document root)  - Server Side
2. PHP Interpreter interprets PHP language and executes instructions as per code.It does not need compilation

PHP Life Cycle


Here is what happens at backend :

We never start any PHP daemon or anything by ourself. When we start Apache, it starts the PHP interpreter along itself PHP is linked to Apache (In general term SAPI i.e. a Server API) using mod_php5.so module PHP as a whole consists of 3 modules (Core PHP, Zend Engine and Extension Layer) Core PHP is the module which handles the requests, file streams, error handling and other such operations 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.

Extensions are a 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 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.

A Step Deeper

But wait! This is still not over yet. Above was just a high level flow diagram. Lets dig a step deeper and see what more is happening behind the scenes:

1. When we start Apache, it also starts PHP interpreter along itself 
2. PHP startup happens in 2 steps
3. 1st step is to perform initial setup of structures and values that persists for the life of SAPI
4. 2nd step is for transient settings that only last for a single page request

Step 1 of PHP Startup

Confused over what’s step 1 and 2 ? No worries, next we will discuss the same in a bit more detail. Lets first see step 1, which is basically the main stuff.
Remember step 1 happens even before any page request is being made.

1. As we start Apache, it starts PHP interpreter
2. PHP calls MINIT method of each extension, which is being enabled. View your 3. php.ini file to see the modules which are being enabled by default
4. 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

A typical MINIT method looks like:

      PHP_MINIT_FUNCTION(extension_name) {

          /* Initialize functions, classes etc */

      }

Step 2 of PHP Startup

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.

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.

RINIT method can be thought as an auto_prepend_file directive, which is pre-appended to every PHP script before execution.

A typical RINIT method looks like:

      PHP_RINIT_FUNCTION(extension_name) {

          /* Initialize session variables, pre-populate variables, redefine global variables etc */

      }

Step 1 of PHP Shutdown

Just like PHP startup, shutdown also happens in 2 steps.

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.

RSHUTDOWN method, destroys the symbols table (memory management) by calling unset() on all variables in the symbols table

A typical RSHUTDOWN method looks like:

      PHP_RSHUTDOWN_FUNCTION(extension_name) {

          /* Do memory management, unset all variables used in the last PHP call etc */

      }

Step 2 of PHP Shutdown

Finally when all requests has been made and SAPI is ready to shutdown, PHP call its 2nd step of shutdown process.

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.

A typical RSHUTDOWN method looks like:

      PHP_MSHUTDOWN_FUNCTION(extension_name) {

          /* Free handlers and persistent memory etc */

      }


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.