PHP working model and running mechanism-PHP Tutorial

Source: Internet
Author: User
PHP working model and operating mechanism. The working model of PHP is very special. To some extent, PHP is essentially different from popular Web technologies such as ASP, ASP. NET, and JSPServlet. Taking Java as an example, in the Web application field, the working model of PHP is very special. To some extent, PHP is essentially different from popular Web technologies such as ASP, ASP. NET, and JSP/Servlet.

Taking Java as an example, Java has two technologies in the Web application field: Java Servlet and JSP (Java Server Page ). Java Servlet is a special type of Java program. it implements related interfaces to process requests sent by Web servers and complete relevant work. JSP is a script similar to PHP in form, but in fact it is finally compiled into Servlet. In other words, in the Java solution, JSP and Servlet are executed as independent Java applications. after initialization, they reside in the memory and communicate with the Web server through specific interfaces, complete the corresponding work. They will not be terminated unless explicitly restarted. Therefore, various caching technologies such as database connection pools can be used in JSP and Servlet.

The mechanism of ASP. NET is similar. As for ASP, although it is also an interpreted language, it still provides an Application object to store global variables at the Application level. it relies on the process where the ASP interpreter resides in IIS, valid throughout the life cycle of the application.

PHP is a pure interpreted script language that can be embedded with HTML on the server. it is especially suitable for developing Web applications. When a PHP script is requested, PHP will read the script and compile it into a Zend operation code. this is a binary representation of the code to be executed. This operation code is then executed and discarded by PHP. The PHP script is initialized each time it is interpreted and stops running after it is interpreted. This operation is independent of each other. each request creates a separate process or thread to explain the corresponding page file. The variables and other objects created on the page are only visible within the current page and cannot be accessed across the page. After the termination, all external resources requested on the page that are not explicitly released by code, including memory, database connection, file handle, and Socket connection, will be forcibly released. That is to say, PHP cannot directly access variables that span pages at the language level, nor can it create objects that reside in memory.

 "; TestStaticVar(); ?> 

In this example, a class named StaticVarTester is defined. it has only one common static member $ StaticVar and is initialized to 0. Then, in the TestStaticVar () function, accumulate StaticVarTester ::$ StaticVar and print it out.

Developers familiar with Java or C ++ should be familiar with this example. $ StaticVar is a static member of the StaticVarTester class and is initialized only when the class is loaded. no matter how many times the StaticVarTester class is instantiated, $ StaticVar only has one instance, it will not be initialized multiple times. Therefore, when the TestStaticVar () function is called for the first time, $ StaticVar is accumulated with a value of 1 and saved. Call the TestStaticVar () function for the second time. The value of $ StaticVar is 2.

The printed results are as expected:

StaticVarTester :: StaticVar = 1 StaticVarTester :: StaticVar = 2 

However, when the browser refreshes the page and executes the code again, different situations may occur. In Java or C ++, the value of $ StaticVar is saved and accumulated. the following result is displayed:

StaticVarTester :: StaticVar = 3 StaticVarTester :: StaticVar = 4 … 

However, in PHP, due to the mechanism described above, every time the current page explains, a program initialization and termination process will be executed. That is to say, StaticVarTester will be reloaded each time it is accessed, and the following statement public static $ StaticVar = 0; will be executed repeatedly. After the page is executed, all the memory space will be recycled, and the $ StaticVar variable (together with the entire StaticVarTester class) will no longer exist. Therefore, no matter how many times the page is refreshed, the $ StaticVar variable returns to the starting point: the variable is initialized to 0 and then accumulated in the TestStaticVar () function call. Therefore, the result we see is always this:

StaticVarTester :: StaticVar = 1 StaticVarTester :: StaticVar = 2 

The advantage of PHP's unique working model is that it basically solves the headache of resource leakage. Web applications are characterized by a large number of concurrent processing in a short time. They frequently apply for and release various resources, which can easily cause leakage or even collapse. The running mechanism of PHP determines that it does not have the conventional crash problem (at most the connection timeout script stops execution). It can be said that PHP is a relatively stable Web application. However, the disadvantages of this mechanism are also very obvious. The most direct consequence is that PHP cannot implement a cross-page buffer mechanism at the language level. The impact of this lack of buffer mechanism can be divided into two aspects:

First, the buffer of the object. As we all know, many design patterns depend on the object buffer mechanism. it takes a lot of time to create and destroy objects because it is necessary to obtain memory resources or other resources to create an object, this is especially true for server software that requires frequent handling of a large number of concurrent requests. Therefore, the lack of object buffering greatly reduces the speed theoretically. Minimize the number of times objects should be created and destroyed to improve the efficiency of the Service Program. because PHP does not currently support multithreading, it cannot compensate for this defect through thread pool scheduling like Java; however, third-party software such as Memcachd can be used to implement the object buffer mechanism of PHP, so as to reduce the time for object creation and destruction to improve the efficiency of the service program. Memcachd caches the PHP compiled operation code and saves the operation code in the memory, and re-uses it the next time it calls the page, which saves a lot of time. EAccelerator is also a commonly used Cache. another popular Alternative tool for eAccelerator is Alternative PHP Cache (APC ).

Second, the buffer for database connection. For MySQL, PHP provides a built-in database buffer mechanism, that is, using mysql_pconnect () instead of mysql_connect () to open the database. PHP automatically recycles obsolete database connections for reuse. In practical applications, such persistent database connections often lead to pseudo-leakage of database connections: At a certain time, too many concurrent database connections exceed the maximum number of connections of MySQL, as a result, new processes cannot connect to the database. However, after a while, when the number of concurrent connections decreases, PHP will release some connections and the website will return to normal again. This occurs because, when pconnect is used, the httpd process of Apache does not release connect, and when the number of httpd processes of Apache exceeds the maximum number of connections of mysql, the connection fails. Therefore, you must carefully adjust the configuration of Apache and Mysql so that the number of httpd processes in Apache does not exceed the maximum number of connections in MySQL. After practice, due to frequent database connections between PHP5 and Oracle10g, the database may also be lost (Oracle has an official PHP enhancement package, I do not know if this problem can be solved. I did not try it ).

The working model of PHP is both a disadvantage and an advantage. in essence, this is the unique feature of PHP.

If you run php in FastCGI mode, parse php. ini, load all extensions, and initialize all data structures. this only happens once when the process starts. An additional benefit is that continuous database connections can work. Nginx + PHP (FastCGI) is a good choice.

Bytes. To some extent, PHP is essentially different from popular Web technologies such as ASP, ASP. NET, and JSP/Servlet. Taking Java as an example, Java in the Web application field ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.