PHP working model and operating mechanism_PHP tutorial

WBOY
Release: 2016-07-13 10:33:43
Original
847 people have browsed it

PHP’s working model is very special. To some extent, there are essential differences between PHP and popular Web technologies such as ASP, ASP.NET, and JSP/Servlet.

Take Java as an example. In the field of Web applications, Java has two technologies: Java Servlet and JSP (Java Server Page). Java Servlet is a special type of Java program that processes requests sent by the Web server and completes corresponding work by implementing relevant interfaces. JSP is a script similar to PHP in form, but in fact, it is finally compiled into a Servlet. That is to say, in Java solutions, JSP and Servlet are executed as independent Java applications. They reside in memory after initialization and communicate with the Web server through specific interfaces to complete corresponding work. They will not terminate unless explicitly restarted. Therefore, various caching techniques such as database connection pooling can be used in JSPs and Servlets.

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

PHP is a purely interpreted scripting language that can be executed on the server side and can embed HTML. It is especially suitable for developing web applications. When a PHP script is requested, PHP reads the script and compiles it into a Zend opcode, which is a binary representation of the code to be executed. This opcode is then executed and discarded by PHP. PHP scripts are initialized each time they are interpreted, and terminate after the interpretation is complete. This operation is independent of each other. Each request will create a separate process or thread to interpret the corresponding page file. Variables and other objects created by the page are only visible within the current page and cannot be accessed across pages. After terminating the operation, external resources applied for in the page and not explicitly released by the code, including memory, database connections, file handles, Socket connections, etc., will be forcibly released. In other words, PHP cannot directly access variables across pages at the language level, nor can it create memory-resident objects.

<?php 
class StaticVarTester { 
	public static $StaticVar = 0; 
} 
   
function TestStaticVar() { 
	StaticVarTester :: $StaticVar += 1; 
	echo "StaticVarTester :: StaticVar = " . StaticVarTester :: $StaticVar; 
} 
   
TestStaticVar(); 
echo "<br / >"; 
TestStaticVar(); 
?> 
Copy after login

In this example, a class named StaticVarTester is defined, which has only one public static member $StaticVar, which is initialized to 0. Then, in the TestStaticVar() function, perform an accumulation operation on StaticVarTester::$StaticVar and print it out.

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

The printed result is as we expected:

StaticVarTester :: StaticVar = 1 
StaticVarTester :: StaticVar = 2 
Copy after login
Copy after login

However, when the browser refreshes the page and executes this code again, a different situation occurs. In Java or C++, the value of $StaticVar will be saved and accumulated. We will see the following results:

StaticVarTester :: StaticVar = 3 
StaticVarTester :: StaticVar = 4 
… 
Copy after login

But in PHP, due to the mechanism mentioned above, every time the current page is interpreted, a program initialization and termination process will be performed. In other words, StaticVarTester will be reloaded every time it is accessed, and the following line of statement public static $StaticVar = 0; will also be executed repeatedly. When the page execution is completed, all memory space will be reclaimed, and the $StaticVar variable (along 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: first 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 
Copy after login
Copy after login

The advantage of PHP’s unique working model is that it basically solves the troublesome resource leak problem. Web applications are characterized by a large number of short-term concurrent processing, and the application and release of various resources are very frequent, which can easily lead to leaks or even crashes. The operating mechanism of PHP determines that it does not have conventional crash problems (at most, the connection timeout script stops execution). It can be said that PHP is a relatively stable web application. However, the shortcomings of this mechanism are also very obvious. The most direct consequence is that PHP cannot implement a cross-page buffering mechanism at the language level. The impact caused by the lack of this buffering mechanism can be divided into two aspects:

One is the buffering of objects. As we all know, many design patterns rely on the buffering mechanism of objects. Creating and destroying objects is very time-consuming, because creating an object requires obtaining memory resources or other more resources. This is especially true for server-side software that needs to frequently cope with large amounts of concurrency. . Therefore, the lack of object buffering will theoretically greatly reduce speed. The number of times that objects are created and destroyed should be reduced as much as possible to improve the efficiency of the service program. Since PHP does not currently support multi-threading, it cannot make up for this flaw through thread pool scheduling like Java; but third-party software such as Memcachd can be used. To implement PHP's object buffering mechanism to reduce the time of object creation and destruction and improve the efficiency of the service program. Memcachd caches the PHP compiled opcode and saves this opcode in memory and reuses it the next time the page is called, which saves a lot of time. The more commonly used cache is eAccelerator. Another popular alternative to eAccelerator is Alternative PHP Cache (APC).

The second is the buffering of database connections. For MySQL, PHP provides a built-in database buffering mechanism, which is to use mysql_pconnect() instead of mysql_connect() to open the database. PHP will automatically recycle abandoned database connections for reuse. In practical applications, this kind of persistent database connection often leads to pseudo-leakage of database connections: at a certain time, there are too many concurrent database connections, exceeding the maximum number of MySQL connections, causing new processes to be unable to connect to the database. But after a while, when the number of concurrency decreases, PHP will release some connections, and the website will return to normal. The reason for this phenomenon is that when using pconnect, Apache's httpd process will not release connect. When the number of Apache's httpd processes exceeds the maximum number of mysql connections, the connection will fail. Therefore, the configuration of Apache and Mysql needs to be carefully adjusted so that the number of Apache's httpd processes does not exceed the maximum number of connections of MySQL. After practice, the author has found that in the connection between PHP5 and Oracle10g, due to frequent database connections, sometimes the database connection will be lost (Oracle officially has an enhancement package for PHP, I don’t know if this problem can be solved, I have not tried it).

PHP’s working model has both disadvantages and advantages. In essence, this is what makes PHP unique.

If you run php in FastCGI mode, parsing php.ini, loading all extensions and reinitializing all data structures only happens once when the process starts. An added bonus is that persistent database connections work. Nginx+PHP (FastCGI) is a good choice.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752422.htmlTechArticlePHP’s working model is very special. To some extent, there are essential differences between PHP and popular Web technologies such as ASP, ASP.NET, and JSP/Servlet. Take Java as an example. In the field of Web applications, Java...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!