Discussion on the operating mechanism of PHP ____php

Source: Internet
Author: User
Tags data structures sapi zend

PHP is easy to say, but mastering is not a simple thing to do. Besides being used, we have to know how it works at the bottom.

PHP is 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.

Learn what the purpose of PHP's underlying implementation is. Dynamic language to be like a good first to understand it, memory management, framework model is worthy of our reference, through the expansion of development to achieve more powerful functions, optimize the performance of our programs.

1. PHP design concept and characteristics

Multi-process Model: Because PHP is a multi-process model, different requests between non-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 like C/S, Java, and C #, PHP is a weakly typed language. The type of a variable is not always fixed at the outset, and it is possible that implicit or explicit type conversions may occur in the runtime, and the flexibility of this mechanism is very convenient and efficient in web development and is detailed in later PHP variables.

Engine (Zend) + component (EXT) mode reduces internal coupling.

The middle tier (SAPI) isolates Web servers and PHP.

The syntax is simple and flexible, without too many specifications. Weaknesses lead to mixed styles, but poor programmers do not write programs that are too outrageous to jeopardize the overall situation.

2. PHP's four-tier system

The core architecture of PHP is the following diagram:

As you can see from the diagram, PHP is a 4-tier system from bottom to top:

Zend Engine: Zend Whole with pure C implementation, is the kernel part of PHP, it will PHP code translation (lexical, parsing and so on a series of compiling process) for the executable opcode processing and implementation of 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 around Zend implementation.

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, etc. are implemented through the extension, Users can also implement their own extension 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.

Upper application: This is our usual PHP program, 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 (engine), ext below the various components are the wheels of the car, SAPI can be seen as a road, the car can run on different types of highways, and a PHP program is the implementation of the car running on the road. Therefore, we need: excellent performance of the engine + the right wheels + the right runway.

3. Sapi

As mentioned earlier, SAPI through a series of interfaces so that external applications can exchange data with PHP and can implement specific processing methods according to different application characteristics, 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 been increasingly used fastcgi+php this year, and is the only way to support asynchronous webserver.

CLI: Application Mode for command-line invocation

4. PHP's execution process &opcode

Let's take a look at the process through which the PHP code executes.

As you can see from the diagram, PHP implements a typical dynamic language execution process: After getting a piece of code, after the 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.

The core of PHP's execution is the translation of a single instruction, or opcode.

OpCode is the most basic unit of PHP program execution. A opcode consists of two parameters (OP1,OP2), return values, and processing functions. PHP programs are ultimately translated into sequential execution of a set of opcode processing functions.

a few common processing functions:

1

Zend_assign_spec_cv_cv_handler: Variable Assignment ($a = $b)

2

Zend_do_fcall_by_name_spec_handler: Function call

3

Zend_concat_spec_cv_cv_handler: string concatenation $a. $b

4

Zend_add_spec_cv_const_handler: addition operation $a +2

5

Zend_is_equal_spec_cv_const: Judgment equal $a ==1

6

Zend_is_identical_spec_cv_const: Judgment equal $a ===1

5. hashtable-Core Data Structure

Hashtable is the core data structure of Zend, in PHP almost used to implement all the common functions, we know that the PHP array is its typical application, in addition, in the Zend interior, such as function symbol table, global variables are also based on hash table to achieve.

PHP's hash table has the following features:

Support for typical key->value queries

Can be used as an array

Add, delete node is O (1) complexity

Key supports mixed types: Simultaneous presence of associative index arrays

Value supports mixed types: Array ("string", 2332)

Supports linear traversal: such as foreach

Zend Hash table implements a typical hash form structure, and provides a forward and backward traversal array function by attaching a bidirectional linked list. The structure of the diagram is as follows:

As you can see, in the hash table, both the Key->value form of the hash structure, as well as bidirectional linked list mode, so that it can easily support fast lookup and linear traversal.

Hash structure: The hash structure of Zend is a typical hash table model, which solves the conflict by the way of linked list. Note that the Zend hash table is a growing data structure that, when the hash table is full, dynamically expands and places the new element in twice-fold mode. The initial size is 8. In addition, in the Key->value fast lookup, the Zend itself has done some optimization, through the space change time to speed up the way. For example, in each element, a variable nkeylength is used to identify the length of the key for quick determination.

Two-way linked list: Zend hash table The linear traversal of the elements is achieved through a linked list structure. Theoretically, do traversal using one-way linked list is enough, the reason for the use of two-way linked lists, the main purpose is to quickly delete, to avoid traversal. Zend hash table is a composite structure that, when used as an array, supports common associative arrays and can be used as sequential index numbers, and even allows for a mixture of 2.

PHP associative arrays: Associative arrays are typical hash_table applications. Once the query process is followed by a few steps (as can be seen from the code), this is a common hash query process and adds some quick decision acceleration lookups. ):

01

Getkeyhashvalue h;

02

Index = n & ntablemask;

03

Bucket *p = Arbucket[index];

04

while (p) {

05

if ((p->h = = h) && (p->nkeylength = = nkeylength)) {

06

Return p->data;

07

}

08

p=p->next;

09

}

10

return falture;

PHP indexed array: An array of indices is our common array, accessed by subscript. For example $arr [0],zend Hashtable is normalized internally, and the hash value and Nkeylength (0) are equally assigned to the index type key. The internal member variable nnextfreeelement is the maximum ID currently assigned, and automatically adds one after each push. It is this normalization process that allows PHP to implement associative and unrelated blending. Because of the particularity of the push operation, the sequence of index key in the PHP array is not determined by the subscript size, but by the push decision. For example $arr [1] = 2; $arr [2] = 3; for a double type key,zend Hashtable will treat him as an index key

6. PHP variables

PHP is a weakly typed language, and it does not strictly differentiate between types of variables. PHP does not need to specify a type when declaring a variable. PHP may perform an implicit conversion of the variable type during the run of the program. As with other strongly typed languages, type conversions can also be displayed in programs. PHP variables can be grouped into simple types (int, string, bool), collection type (array resource object), and constants (const). All of the above variables are the same structure zval at the bottom.

Zval is another very important data structure in Zend that identifies and implements PHP variables with data structures as follows:

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.