PHP working model and operating mechanism

Source: Internet
Author: User
Tags terminates

PHP's working model is very special. To some extent, PHP and ASP, ASP, Jsp/servlet and other popular web technologies, there is a fundamental difference.   Java, for example, has two technologies in Web applications: Java servlet and JSP (Java Server Page). Java servlet is a special type of Java program, which implements the relevant interface, handles the request sent by the Web server, and completes the corresponding work. JSP is formally a PHP-like script, but in fact it is eventually compiled into a servlet. In other words, in a Java solution, JSPs and Servlets are executed as standalone Java applications that reside in memory after initialization and communicate through specific interfaces and Web servers to do the job. They do not terminate unless they are explicitly restarted. As a result, various caching techniques, such as database connection pooling, can be used in JSPs and Servlets.  asp. NET is similar to this. As for ASP, although it is also an interpreted language, it still provides the application object to hold the application-level global variables, which depend on the process that the ASP interpreter resides in IIS and is valid throughout the lifetime of the application.  php is a purely explanatory type of scripting language that can be embedded in HTML on the server, especially 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 by PHP and discarded. The php script initializes each time it is interpreted and terminates the operation after the explanation is complete. This operation is independent of each other, and each request creates a separate process or thread to interpret the corresponding paging file. The variables and other objects created by the page are visible only within the current page and cannot be accessed across pages. After termination, external resources, including memory, database connections, file handles, socket connections, and so on, that are requested on the page and not explicitly released by the code, are forcibly released. In other words, PHP cannot implement direct access to variables across pages at the language level, nor can it create objects that reside in memory.  class staticvartester { public static $StaticVar = 0;}  function Teststaticvar () { staticvartester:: $StaticVar + = 1; echo "Staticvartester:: StaticVar = ". Staticvartester:: $StaticVar;  } teststaticvar ();  echo "
";  teststaticvar ();  In this example, a class named Staticvartester is defined, which has only one public static member $staticvar and is initialized to 0. Then, in the Teststaticvar () function, add an aggregate operation to the Staticvartester:: $StaticVar and print it out.   Developers familiar with Java or C + + should not be unfamiliar with this example. $StaticVar, as a static member of the Staticvartester class, is initialized only when the class is loaded, no matter how many times the Staticvartester class is instantiated, $StaticVar has only one instance and is not initialized more than once. Therefore, when the Teststaticvar () function is called for the first time, the $StaticVar is incremented, with a value of 1 and saved. The second time the Teststaticvar () function is called, the value of $StaticVar is 2.   printed out the same results as we expected:  1 staticvartester:: Staticvar = 12 staticvartester:: Staticvar = 2 However, when the browser refreshes the page, When you execute this code again, different situations occur. In Java or C + +, the value of $StaticVar is saved and continues to accumulate, and we will see the following result:  1 staticvartester:: Staticvar = 32  Staticvartester:: Staticvar = 4  but in PHP, because of the mechanism described above, the current page is interpreted every time the program initializes and terminates the process. That is, each time the access, the Staticvartester will be reloaded, and the following line statement public static $StaticVar = 0; will also be repeated. When the page executes, all of the memory space is reclaimed, and $StaticVar this variable (along with the entire Staticvartester Class) is no longer present. Therefore, no matter how many times the page is refreshed, $StaticVar variable goes back to the starting point: it is initialized to 0, and then is incremented in the Teststaticvar () function call. So, the result we see is always this:  1 staticvartester:: Staticvar = 12 STATICVArtester:: Staticvar = 2PHP The advantage of this unique work model is that The impact of this lack of buffering mechanism can be divided into two aspects:  one is the buffer of the object. It is well known that many design patterns rely on the buffering mechanism of objects, and that it is time consuming to create and destroy objects, because creating an object to acquire memory resources or other resources is especially true for server-side software that requires frequent handling of large amounts of concurrency. Therefore, the loss of object buffering, in theory, will greatly reduce the speed. As much as possible to reduce the number of objects created and destroyed to improve the efficiency of the service program, because PHP currently does not support multi-threading, it can not be like Java through the thread pool scheduling to compensate for this flaw, but the use of third-party software such as Memcachd to implement the PHP object buffering mechanism, Increase the efficiency of your service programs by reducing the time it takes to create and destroy objects. Memcachd will save a lot of time by caching the php compiled opcode and saving the opcode in memory and reusing it the next time the page is called. More commonly used caches have eaccelerator, and another popular Eaccelerator replacement tool is alternative PHP cache (APC).   Two is the buffer of database connection. For Mysql,php provides a built-in database buffering mechanism that uses mysql_pconnect () instead of mysql_connect () to open the database. PHP automatically reclaims discarded database connections for reuse. In real-world applications, this persistent database connection often leads to a pseudo-leak of database connectivity: At some point, there are too many concurrent database connections that exceed the maximum number of MySQL connections, causing the new process to fail to connect to the database. But over time, when the number of concurrent numbers decreases, PHP releases some connections and the site returns to normal. This behavior occurs because Apache's httpd process does not release connect when using Pconnect, and when the number of Apache httpd processes exceeds the maximum number of MySQL connections, there is an inability to connect. Therefore, you need to carefully adjust the configuration of Apache and MySQL so that the number of Apache httpd processes does not exceed the maximum number of MySQL connections. The author through practice, in PHP5 and oracle10g connection, due to the frequency of the database connection, sometimes there will be a loss of database connection (Oracle has an enhanced package for PHP, I do not know whether it can solve this problem, the author did not try).  php's work model is a disadvantage and an advantage, which is, in essence, the uniqueness of PHP.   If you run PHP in fastcgi mode, parse php.ini, load all extensions, and initialize all of the data structures only once when the process starts. An additionalThe advantage is that persistent database connections can work. Nginx+php (FastCGI) is a good choice.

PHP working model and operating mechanism

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.