PHP meditation-PHP source code

Source: Internet
Author: User
Ec (2); this is the first article in a series. Currently, I think of the following topics: SQL Injection problem event model AOP model UIFramework implementation Template mechanism PHP meditation recording work model PHP work model is very special. To some extent, PHP is essentially different from popular Web technologies such as ASP, ASP. NET, and JSPServlet. Using Jav script ec (2); script




This is the first article in a series. The other topics currently come to mind:
  
SQL Injection Problems
Event Model
AOP Model
Implementation of the UI Framework
Template Mechanism
  
  
PHP meditation
Work Model
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 not like this at all. As a pure explanatory language, PHP scripts initialize each time they are interpreted and terminate the execution after the interpretation. 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.

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 the variables that span pages at the language level, nor can it create objects that reside in the memory. See the following example:
  
   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. 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 line of statements
  
Public static $ StaticVar = 0;
  
It will also 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.

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.