search
HomeBackend DevelopmentPHP TutorialIn-depth understanding of PHP kernel (2) Overview - PHP life cycle and Zend engine, -phpzend_PHP tutorial

In-depth understanding of the PHP kernel (2) Overview - PHP life cycle and Zend engine, -phpzend

This article refers to "In-depth understanding of the PHP kernel", address: https://github .com/reeze/tipi

1. SAPI interface

SAPI (Server Application Programming Interface) refers to the programming interface for specific PHP applications. PHP scripts can be executed in many ways: through a web server, the command line, or embedded in other programs.

Although executing scripts through a web server and a command line program looks very different, the actual process is the same. It's just that the web server method returns the php script execution results to the web server, and the command line prints the results to the console.

Script execution starts with the SAPI interface implementation, but different SAPI interface implementations will complete their specific work. For example, Apache’s mod_php SAPI implementation needs to initialize some information obtained from Apache. In the output content, the content Returned to Apache, other SAPI implementations are similar

2. Beginning and ending

(1) PHP will go through two stages after it starts executing: the starting stage before processing the request and the ending stage after the request. There are two processes in the beginning phase: the first process is the module initialization phase (MINIT). During the entire SAPI life cycle (such as the entire life cycle after Apache starts or the entire execution process of the command line program), this process only Do it once . The second process is the module activation phase (RINIT), which occurs in the request phase . For example, if a page is requested through a URL, module activation (RINIT request starts) will be performed before each request. For example, if PHP registers some extension modules, it will call back the MINIT function of all modules in the MINIT stage. The module performs some initialization work at this stage, such as registering constants, defining classes used by the module, etc. The module can implement these callback functions through the following macros during implementation:

<span>PHP_MINIT_FUNCTION(myphpextension)
{
    </span><span>//</span><span> 注册常量或者类等初始化操作</span>
    <span>return</span><span> SUCCESS;      
}</span>

The basic environment of the PHP initialization script after the request arrives, such as creating an execution environment, including a symbol table that saves the variable names and value contents during PHP running, as well as a symbol table of all current functions, classes and other information. Then PHP will call the RINIT function of all modules. At this stage, each module can also perform some related operations. The module RINIR function is similar to the MINIT callback function:

<span>PHP_RINIT_FUNCTION(myphpextension)
{
    </span><span>//</span><span> 例如记录请求开始时间
    </span><span>//</span><span> 随后在请求结束的时候记录结束时间,这样我们就能记录下处理请求所花费的时间了</span>
    <span>return</span><span> SUCCESS;
}</span>

After the request is processed, it enters the end phase. Generally, PHP will enter the end phase when the script is executed to the end or by calling the die() or exit() function. The end phase also has two links: one is to deactivate the module after the request is completed (RSHUTDOWN, corresponding to RINIT), and the other is to close the module when the SAPI life cycle ends (the web server exits or the command line script is executed and exits) (MSHUTDOWN, corresponding to MINIT).

<span>PHP_RSHUTDOWN_FUNCTION(myphpextension)
{
    </span><span>//</span><span> 例如记录请求结束时间,并把相应的信息写入到日志文件中</span>
    <span>return</span><span> SUCCESS;
}</span>

(2) Single-process SAPI life cycle

CLI/CGI mode PHP belongs to the single-process SAPI mode. This type of request is closed after processing the request once. SAPI life cycle: start-request start-request close-end

(3) Multi-process SAPI life cycle

Usually PHP is compiled into a module of Apache. Apache usually adopts multi-process mode. After Apache is started, it will fork multiple child processes. Each process has an independent memory space. Each child process will go through the start and end stages, but each The initial phase of the process only occurs after the process forks out, and it may be possible to process multiple requests during the entire process life cycle. The shutdown phase is only performed after Apache is shut down or the process is terminated. In these two phases, the request will be repeated with each request - the request shutdown link

(4) The multi-thread mode is similar to a process in a multi-process. The difference is that the request start-request closure process will be repeated in parallel throughout the life cycle of the process.

3. Zend Engine

Zend engine is the core of PHP implementation and provides the infrastructure for language implementation. For example, the implementation of PHP syntax, script compilation and running environment, expansion mechanism and memory management, etc. PHP here refers to the official PHP implementation (there is also Facebook's Hiphop, which has now developed into HHVM. So far (this project) PHP does not have a standard language specification), while PHP provides request processing and other Web server interface (SAPI)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1095119.htmlTechArticleIn-depth understanding of the PHP kernel (2) Overview - PHP life cycle and Zend engine, -phpzend This article refers to "In-depth understanding PHP Kernel", address: https://github.com/reeze/tipi 1. SAPI interface SAPI (Serve...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php文件如何在手机上打开php文件如何在手机上打开Nov 13, 2023 am 11:08 AM

要在手机上打开php文件,需要在手机上搭建一个能够运行php的服务器环境,并将php文件上传到服务器上。然后,在手机上的浏览器中输入服务器的IP地址或域名,加上php文件的路径,即可打开php文件并查看其内容。

如何处理PHP文件路径大小写错误并生成相应的报错信息如何处理PHP文件路径大小写错误并生成相应的报错信息Aug 08, 2023 pm 09:45 PM

如何处理PHP文件路径大小写错误并生成相应的报错信息在开发PHP程序的过程中,我们经常会遇到文件路径大小写错误的问题。由于Windows和Linux系统对文件路径的大小写处理方式不同,当程序在开发环境中使用Windows系统测试通过后,在部署到Linux服务器上时可能会导致路径错误。为了解决这个问题,我们可以通过一些方法来处理文件路径的大

.php文件中可包含几方面内容.php文件中可包含几方面内容Mar 06, 2023 pm 05:32 PM

php文件中可包含的内容:1、起始标签“<?php”和结束标签“?>”,所有PHP代码都必须写在这对标签的里面;2、分号“;”,是PHP语句的分隔符,也代表着代码执行的指令;3、注释,有单行注释“//”、多行注释“/* */”、Shell注释“#”三种风格注释;4、换行符,可加强代码的可读性;5、代码段(如函数等)。

如何才能打开php文件如何才能打开php文件Sep 01, 2023 am 11:53 AM

打开php文件步骤:1、选择文本编辑器;2、在选择的文本编辑器中,创建一个新的文件,并将其保存为.php文件;3、在创建的PHP文件中,编写PHP代码;4、要在本地计算机上运行PHP文件,需要设置一个服务器环境;5、安装服务器环境后,需要将PHP文件放入服务器目录中;6、一旦将PHP文件放入服务器目录中,就可以通过浏览器来运行它。

怎么打开php文件怎么打开php文件Jan 24, 2024 pm 04:12 PM

打开php文件的工具:1、Notepad++;2、Sublime Text;3、Visual Studio Code;4、Eclipse;5、XAMPP。详细介绍:1、Notepad++,这是一个免费的文本编辑器,支持多种编程语言,包括PHP,它具有语法高亮和代码折叠等功能,可以更轻松地阅读和编辑PHP代码;2、Sublime Text,这是一款功能强大的文本编辑器等等。

运行PHP程序的前提条件是什么?运行PHP程序的前提条件是什么?Mar 26, 2024 pm 03:45 PM

标题:运行PHP程序的前提条件及示例PHP是一种广泛应用于Web开发的脚本语言,许多网站都是通过PHP来运行其动态内容。要成功运行PHP程序,必须满足一些前提条件。下面将介绍运行PHP程序的前提条件,并提供具体的代码示例。服务器环境首先,PHP程序需要在支持PHP语言的服务器环境中才能正常运行。最常见的服务器环境是Apache服务器,并且需要安装PHP解释器

PHP文件可包含哪些代码PHP文件可包含哪些代码Aug 01, 2023 pm 01:34 PM

PHP文件可包含的代码:1、PHP代码,用来完成各种服务器端的任务;2、HTML代码,用来定义网页的结构和布局;3、CSS代码,用来定义网页的样式;4、JavaScript代码,用来实现在网页中进行各种动态交互的功能;5、SQL代码,用来操作数据库;6、文件包含代码,可以将代码进行模块化;7、第三方库和框架,可以快速构建功能丰富的应用程序。

如何处理PHP文件编码错误并生成相应的报错信息如何处理PHP文件编码错误并生成相应的报错信息Aug 06, 2023 pm 02:09 PM

如何处理PHP文件编码错误并生成相应的报错信息在开发PHP应用程序时,经常会遇到文件编码错误的问题。这些错误可能会导致程序无法正常运行,或者在用户面前展示乱码。为了更好地处理这些错误并生成相应的报错信息,我们可以采取一些常见的解决方案。确定文件编码首先,我们需要确定文件的编码格式。常见的编码格式有UTF-8、GBK等。可以通过文本编辑器的“另存为”功能来查看

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools