Deep understanding of PHP's underlying mechanism (-)

Source: Internet
Author: User
Tags array error handling execution hash variables php and php code zend

As a dynamic language, how PHP is implemented, its underlying mechanism, what kind of characteristics, this article introduces the PHP design concept, the overall structure, core data structure and variables related to the underlying knowledge, to our better development of PHP programs, optimize performance and so have a certain guiding significance. tagphp bottom mechanism performance optimization directory 1, overview ... 1what is PHP? 1 Understand the purpose of its underlying implementation? 12, PHP design concept and characteristics ... 13, PHP four-tier system ... 24, Sapi 35, PHP implementation process &opcode. 56, hashtable-core Data structure ... 67, PHP variables ... 8 Overview ... 8Zval 9 integer, floating-point type variable ... 10 string variable ... 10 array variable ... 11 Resource type variable ... Scope of 11PHP variables ... 121, overview What is PHP? A dynamic language suitable for web development. Specifically, it is a software framework that contains a large number of components in C language. More narrowly, you can think of it as a powerful UI framework. Understand the purpose of its underlying implementation? A dynamic language is like a good thing to know it first.

Memory management and framework model are worthy of our reference

Improve the performance of our programs by expanding development to achieve more powerful functions

2, PHP design concept and characteristics of the multi-process model

Because PHP is a multiple process model, different requests between the interference, so as to ensure that a request to hang out will not affect the overall service, of course, with the development of the Times, PHP has already supported the multithreaded model. Weakly typed language

Unlike languages such as C + +, Java, and C #, PHP is a weakly typed language: The type of a variable is not fixed at the outset, and it is only possible to determine and possibly occur implicit or explicit type conversions in the runtime, which is very convenient and efficient in web development, Detailed in the following PHP variables. Engine (Zend) + component (EXT) mode reduces internal coupling

Middle tier (SAPI) isolate Web server and PHP

The syntax is simple and flexible, without too many specifications. (resulting in mixed style)

Poor programmers will not write programs that are too outrageous to jeopardize the overall situation.

3, PHP's four-tier system PHP's core architecture is the following figure

Figure 1 PHP structure from the diagram can be seen, PHP from bottom to top is a 4-tier system Zend Engine

Zend Whole with pure C implementation, is the kernel part of PHP, it will PHP code translation (lexical, parsing, such as a series of compiling process) for the implementation of opcode processing and implement the corresponding processing methods, the implementation of the basic data structure (such as Hashtable, OO), memory allocation and management, Provides the corresponding API method for external call, is the core of all, all the peripheral functions are implemented around Zend. Extensions

Around the Zend Engine, extensions provides a variety of basic services through a modular approach, our common built-in functions (such as array series), standard libraries, and so on are implemented through extension, and users can also implement their own extension as needed to achieve functional expansion , performance optimization and other purposes (such as paste is using the PHP middle tier, Rich text parsing is the typical application of extension). Sapi

SAPI full name is the server application programming Interface, which is the service-side application programming interface, SAPI through a series of hook functions, so that PHP can interact with the perimeter data, which is a very elegant and successful PHP design, By SAPI the PHP itself and the top application decoupling isolation, PHP can no longer consider how to be compatible with different applications, and the application itself can be implemented in accordance with their own characteristics of different ways. The upper application is described in the SAPI section later

This is what we usually write PHP programs, through different sapi ways to get a variety of application patterns, such as through the webserver to implement Web applications, the command line to run the script and so on. If PHP is a car, then the frame of the car is PHP itself

Zend is the engine of the car (engine)

The various components below ext are the wheels of the car

SAPI can be seen as highways, cars can run on different types of highways.

And the execution of a PHP program is that the car runs on the road. Therefore, we need: Excellent engine + suitable wheels + right runway 4, sapi as mentioned above, SAPI through a series of interfaces so that external applications can exchange data with PHP and can be implemented according to different application characteristics of specific processing methods, Some of our common SAPI are: Apache2handler

This is the use of Apache as a webserver, using the mod_php mode of operation, is now the most widely used. Cgi

This is another direct interaction between Webserver and PHP, the famous fastcgi protocol, which has fastcgi+php more and more applications this year, and is the only way to support asynchronous webserver. For fastcgi and mod_php, see another article, "PHP Performance research-mod_php vs fastcgi" cli

The definition and main interface functions of the application Mode SAPI of command line invocation are as follows

Figure 2 SAPI protocol Here are some of the main functions startup:php initialized when invoked

For example, CGI mode, which loads all the extension and performs module initialization at startup. Closing work when shutdown:php closes

Activate: request initialization

Dectivate: Closing the work at the end of the request

Ub_write: Specify how data is to be exported

For example Apache2handler way, because PHP as a so existence of Apache, so its output is called Apache Ap_write function, and in the CGI mode, the system call write. Sapi_error: Error handling function

Read_post: Reading Post data

Register_server_variables: Registering environment variables into $_server

This is generally registered according to the different protocol criteria of the variable. 5, PHP implementation process &opcode Let's take a look at the process of executing the PHP code.

Figure 3 The PHP code execution process can be seen from the diagram, PHP implements a typical dynamic language execution process: After getting a piece of code, after lexical parsing, parsing, and so on, the source program will be translated into one instruction (opcodes), and then zend the virtual machine in sequence to perform these instructions to complete the operation. PHP itself is implemented in C, so the final call is also the function of C, in fact, we can think of PHP as a C developed software. It is not difficult to see through the above description, PHP's implementation of the core is a translation of a directive, that is, Opcodeu Opcodeopcode is the most basic unit of PHP program execution. A opcode consists of two parameters (OP1,OP2), return values, and processing functions. The PHP program is eventually translated into a set of opcode processing functions the sequential execution of several common processing functions zend_assign_spec_cv_cv_handler: variable allocation ($a = $b) zend_do_fcall_by_name_spec_ HANDLER: Function call Zend_concat_spec_cv_cv_handler: string concatenation $a. $b Zend_add_spec_cv_const_handler: addition operation $a +2 Zend_is_equal_spec _cv_const: Judge equal $a ==1 zend_is_identical_spec_cv_const: Judge equal $a ===16, hashtable-core data structure HashTable is the core data structure of ZEND, In PHP is almost used to implement all the common functions, we know that the PHP array is its typical application, in addition, in the Zend, such as function symbol table, global variables are also based on hash table to implement. PHP's hash table has the following features: support for typical key->value queries




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.