PHP 7 source code learning sequence & Chapters 1 and 2

PHP 7 source code learning sequence & Chapters 1 and 2

Zero-sequence

I do not want to say, if insisted on not down yet.
Safety is common sense do not make use of the code, so often from the bottom to bypass the developers think security protection. Audit and the ability to see the source code that I think very important, especially when new vulnerabilities research, and sometimes the answer is hidden in the source code. I hope that through trying to learn PHP source code to learn how to look at the source, on the other hand to deepen the understanding of the mechanism of PHP. I hope not dove out of it ~

The first chapter PHP 7 Overview

1. A brief history of PHP

  • In 1996 Rasmus Lerdorf released version 2.0 of the relatively well
  • In early summer 2000, version 4.0 release, Zend engine official debut, 3.0, there can be up to 10 times the performance compared to
  • In July 2004, PHP 5 official release, upgrade to Zend Engine 2.0. PHP 5 biggest feature is the introduction of object-oriented mechanism for all types prompts and exception handling mechanism
  • In 2005 PHP community project initiated by PHP 6, but canceled due to various reasons. But the number of new features continue to be added to the PHP 5.x versions, such as namespaces, anonymous functions, closures, etc.
  • The summer of 2015, PHP 7 released the first Alpha version, has a very big innovation, in particular to significantly boost performance.

2. PHP 7 and debugging tools

  • GDB
    $ gdb php (gdb) b main (gdb) r test.php ...
  • vld extension
    PHP code execution actually perform various opcode after the code analysis. It is easy to see that the implementation process by vld opcode extension.
    $ php -dvld.active=1 vld.php
  • Source Insight
    under a Windows IDE, I use this to learn the source code

Chapter acquaintance PHP 7 Source overall framework

1. Perform the principle PHP 7 languages

  • Step 1: Token source obtained through morphological analysis
    step Re2C implemented using lexical analyzer
  • 第2步:基于语法分析器生成抽象语法树(AST)
    语法分析器基于Bison实现。语法分析使用了BNF(Backus-Naur Form,巴科斯范式)来表达文法规则,Bison借助状态机、状态转移表和压栈、出栈等一系列操作,生成抽象语法树。
  • 第3步:抽象语法树转换为opcodes(opcode指令集合),PHP解释执行opcodes。
    opcode是PHP 7 定义的一组指令标识,指令对应着相应的handler(处理函数)。当虚拟机调用opcode,会找到opcode背后的处理函数,执行真正的处理。例如:echo对应的opcode是ZEND_ECHO

2.PHP 7 内核架构

  • Zend引擎(核心)
    词法/语法分析、ASt编译和opcodes的执行均在Zend引擎中实现。此外,变量设计、内存管理、进程管理都在这实现
  • PHP层
    处理来自外部的交互
  • SAPI
    Server API,其中包含了常见的cli SAPI和fpm SAPI。PHP定义好输入/输出规范,依据此规范与PHP交互的一方都可以称为Server
  • 扩展部分

3.PHP 7 源码结构初步介绍

sapi

sapi目录是对输入和输出层的抽象,是PHP提供对外服务的规范
常见SAPI包括:

  • apache2handler:Apache扩展
  • cgi-fcgi:编译后生成支持CGI协议的可执行程序
  • fpm-fcgi:PHP官方提供的FastCGI进程管理器
  • cli:命令行交互接口

Zend

重点部分包括

  • 内存管理模块
  • 垃圾回收
  • 数组实现

main

main目录是SAPI层和Zend层的粘合剂,起到承上启下的作用。

ext

PHP扩展相关的目录

TSRM

Thread Safe Resource Manager----线程安全资源管理器

Guess you like

Origin www.cnblogs.com/20175211lyz/p/12205386.html