Operating mechanism and the underlying principles of php

Concept and design characteristics php

  • PHP is designed to be suitable for the development of a dynamic web scripting languages, having both interpreted and weak type, underlying entirely by the C language.
    • Interpretive program that is running side edge explanation, line by line run.
    • I.e. a weakly typed variable type is not determined to start, implicit or explicit conversion may occur during operation, which is the underlying structure zval php determined, the flexibility of this mechanism is very convenient in web development, efficient.
  • Multi-process model: Because PHP is a multi-process model, non-interference between different requests, to ensure that such a request would not hang up the overall service impact, of course, with the development of the times, PHP also already supports multi-threading model.

  • Engine (Zend) + mode component (EXT) of the internal coupling decreases.

  • The intermediate layer (SAPI) isolated web server and PHP.

  • The syntax is simple and flexible, not too many specifications.

PHP system of four

PHP core architecture as shown below:

As can be seen from the figure, PHP is a four layer system from the lower to:

  • Zend Engine: Zend integrally implemented in pure C, PHP is a core part, it will PHP code translation (lexical, and a series of parsing the compilation process) and a process to perform opcode implement corresponding processing methods to achieve the basic data structures (such as hashtable, oo), memory allocation and management, to provide the corresponding api method for external calls, the core of everything, all the peripheral functions are around the Zend implementation.

  • Extensions: around the Zend engine, extensions provide a variety of basic services through modular way, our common variety of built-in functions (such as array series), standard libraries are implemented by extension, users can achieve according to their own needs extension to achieve function expansion, performance optimization purposes (PHP paste it as an intermediate layer in use, rich text parsing typical application is the extension).

  • Sapi: Sapi stands for Server Application Programming Interface, which is server-side application programming interface, Sapi by a series of hook function, so that PHP can interact with data and peripherals, it is very elegant and PHP success of a design by the success of the PHP sapi decoupled itself and the upper application isolation, PHP can no longer consider how compatible for different applications, and the application itself can also achieve a different approach for its own characteristics.

  • The upper application: This is what we usually write PHP applications, get a variety of applications in different modes sapi ways, such as by implementing a web application webserver, at the command line to run as scripts and so on.

If PHP is a car, then the car itself is the PHP framework, Zend is the car's engine (engine), Ext following the various components that car wheels, Sapi can be seen as the highway, the car can run on different types of road on a PHP program execution while the car is running on the road. Therefore, we need: excellent performance of engines + + right wheel right track.

Cow

    如前所述,Sapi通过通过一系列的接口,使得外部应用可以和PHP交换数据并可以根据不同应用特点实现特定的处理方法,我们常见的一些sapi有:
  • apache2handler: This is apache as a webserver, using mod_PHP mode processing run time, it is now the most widely used one.
  • cgi: This is a webserver and PHP another direct interaction, which is famous fastcgi protocol, in the last year fastcgi + PHP get more and more applications, also asynchronous webserver are only supported way.
  • cli: command line mode application calls

PHP execution process & opcode

我们先来看看PHP代码的执行所经过的流程。

从图上可以看到,PHP实现了一个典型的动态语言执行过程:拿到一段代码后,经过此法解析、语法解析等阶段后,源程序会被翻译成一个个指令(opcodes),然后Zend虚拟机顺次执行这些指令完成操作。PHP本身是用C实现的,因此最终调用的也都是C的函数,实际上,我们可以把PHP看做是一个C开发的软件。
  • The core of PHP execution of a single instruction is translated, that opcode
  • PHP opcode is the basic unit of program execution. An opcode by two parameters (op1, op2), the function return value and the composition process. PHP is finally performed Cheng Hoon translated into a set of opcode order processing function.

  • Several common handler
PHP
 
1    ZEND_ASSIGN_SPEC_CV_CV_HANDLER : 变量分配 ($a=$b)
2    ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER:函数调用
3    ZEND_CONCAT_SPEC_CV_CV_HANDLER:字符串拼接 $a.$b
4    ZEND_ADD_SPEC_CV_CONST_HANDLER: 加法运算 $a+2
5    ZEND_IS_EQUAL_SPEC_CV_CONST:判断相等 $a==1
6    ZEND_IS_IDENTICAL_SPEC_CV_CONST:判断相等 $a===1

Hash Table - core data structure

HashTable is Zend core data structure, which in PHP is used to realize almost all common features, we know PHP array that is typical application, moreover, in interior Zend, as a function of the symbol table, the global variables are also based on Hash Table to achieve.

  • PHP的hash table 具有如下特点:
    • 支持典型的key——> value查询
    • 可以当做数组使用
    • 添加、删除节点是 O(1)复杂度
    • key 支持混合类型:同时存在关联数组和索引数组
    • value 支持混合类型:array("string", 23332)
    • 支持线性遍历:如 foreach

Zend Hash Table 实现了典型的hash表散列结构,同时通过附加一个双向链表,提供了正向、反向遍历数组的功能。其结构如下图:

可以看到, 在hash table中既有 key -> value 形式的散列结构, 也有双向链表模式,使得它能够非常方便的支持快速查找和线性遍历。

  • 散列结构:Zend的散列结构是典型的hash表模型,通过链表的方式来解决冲突。需要注意的是zend的hash table 是一个自增长的数据结构,当hash表数目满了之后,其本身会动态以2倍的方式扩容并重新元素位置。初始大小均为8。另外,在进行key->value快速查找时候,zend本身还做了一些优化,通过空间换时间的方式加快速度。比如在每个元素中都会用一个变量nKeyLength标识key的长度以作快速判定。

  • 双向链表:Zend hash table通过一个链表结构,实现了元素的线性遍历。理论上,做遍历使用单向链表就够了,之所以使用双向链表,主要目的是为了快速删除,避免遍历。Zend hash table是一种复合型的结构,作为数组使用时,即支持常见的关联数组也能够作为顺序索引数字来使用,甚至允许2者的混合。

  • PHP关联数组:关联数组是典型的hash_table应用。一次查询过程经过如下几步(从代码可以看出,这是一个常见的hash查询过程并增加一些快速判定加速查找。):
getKeyHashValue h;
index = n & nTableMask;
Bucket *p = arBucket[index];
while (p) {
if ((p->h == h) & (p->nKeyLength == nKeyLength)) {
RETURN p->data;  
}
p=p->next;
}
RETURN FALTURE;
  • PHP索引数组:索引数组就是我们常见的数组,通过下标访问。例如 $arr[0],Zend HashTable内部进行了归一化处理,对于index类型key同样分配了hash值和nKeyLength(为0)。内部成员变量nNextFreeElement就是当前分配到的最大id,每次push后自动加一。正是这种归一化处理,PHP才能够实现关联和非关联的混合。由于push操作的特殊性,索引key在PHP数组中先后顺序并不是通过下标大小来决定,而是由push的先后决定。例如 $arr[1] = 2; $arr[2] = 3;对于double类型的key,Zend HashTable会将他当做索引key处理

PHP 变量

PHP是一门弱类型语言,本身不严格区分变量的类型。PHP在变量申明的时候不需要指定类型。PHP在程序运行期间可能进行变量类型的隐示转换。和其他强类型语言一样,程序中也可以进行显示的类型转换。PHP变量可以分为简单类型(int、string、bool)、集合类型(array resource object)和常量(const)。以上所有的变量在底层都是同一种结构 zval。

Zval是zend中另一个非常重要的数据结构,用来标识并实现PHP变量,其数据结构如下:

Zval 主要由三部分组成:

  • type:指定了变量所述的类型(整数、字符串、数组等)
  • refcount&is_ref:用来实现引用计数
  • value:核心部分,存储了变量的实际数据

Zvalue是用来保存一个变量的实际数据。因为要存储多种类型,所以zvalue是一个union,也由此实现了弱类型。

PHP变量类型和其实际存储对应关系如下:

IS_LONG   -> lvalue
IS_DOUBLE -> dvalue
IS_ARRAY  -> ht
IS_STRING -> str
IS_RESOURCE -> lvalue

Guess you like

Origin www.cnblogs.com/lz0925/p/11122493.html