Learn PHP kernel with factory Manager (c): Source directory structure

Source: Internet
Author: User
Tags learn php sapi zend

In the previous article we have introduced the Source Code analysis tool installation, configuration and debugging methods, this article we will describe the PHP source directory structure.

I. Catalogue overview

As php-7.0.12 An example, read the source of the students should find the source directory as many as more than 10, the following is a description of each directory.

Catalogue Description
Tsrm Thread-related security implementations
Zend The core implementation of PHP parser
Build Linux compilation-related directories
Ext Extension of PHP
Main PHP's main code
NetWare Network directory, socket definition and implementation
Pear Code repositories for PHP extensions and applications
Sapi Application layer Interface for PHP
Scripts Script directory under Linux
Tests Test Script Directory
Travis For building, non-PHP specific directories
Win32 Related scripts for compiling PHP under Windows
II. Core Catalogue

Although the source directory is numerous, but the core directory is only sapi、main、zend、ext、TSRM .

2.1, SAPI

Do you remember the PHP application scenario? We can access PHP from the command line, or we can access the PHP script through the browser, even embedded in the microcontroller for C, C + + program calls. They correspond to the CLI mode, the fpm/cgi mode, the embed mode, in addition to the Apache2handler, Litespeed mode.

SAPI Full Name Server API, responsible for PHP external service specification, it defines the structure sapi_module_struct , the structure defines the mode start, close, activation, failure and other hooks function pointers, each mode of these function pointers to their own functions, you can easily expand the way PHP external services. Several of the above models are implemented sapi_module_strcut only to complete the multi-scene application of PHP.

2.2. Main

The purpose of the main directory is to undertake SAPI requests, analyze the script files and parameters to execute, and initialize the environment and configuration, such as initializing variables and constants, registering functions, parsing profiles, loading extensions, and so on.

2.3, Zend

Zend Directory is known as the Zend Engine, is the most core of PHP, the main responsibility for PHP syntax implementation, memory management and script compilation run environment, it is composed of compilers, actuators two parts.

The compiler is responsible for the lexical, syntactic parsing of PHP code, and generate an abstract syntax tree, and then further compiled to Opcode,opcode is the Zend virtual machine can be recognized instructions, PHP7 altogether has 173 opcode, all the syntax is composed of these opcode. The executor is responsible for executing the opcode of the compiler output.

2.4, ext

EXT is the abbreviation of extension, it is a way to extend PHP kernel function, it is divided into PHP extension and Zend Extension, both support user custom development, both are more common, PHP extension has GD, JSON, date, array, etc. And the Opcache we know is the Zend extension.

2.5, TSRM

The full name of TSRM is called thread safe Resource Manager, which is the threading Security resource manager.

We know that global variables are variables that are defined outside the function, which are public resources, and that access to public resources in a multithreaded environment can cause conflicts, and TSRM is created to solve the problem. It assigns a separate self-increment ID to each thread, which is the index of the current thread's global variable memory area, which enables the thread to be completely independent.

In fact, most of PHP sapi are single-threaded, so there is no need to pay too much attention to thread safety, but in Apache or the user's own implementation of the PHP environment, you need to consider thread safety issues.

Learn PHP kernel with factory Manager (c): Source directory structure

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.