[精选] php是怎么样工作的呢?它的生命周期是怎么样的呢?
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">举荐</span>关注</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">以下请在本公众号里操作</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「<span style="color: black;">微X</span>群」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">加入PHP技术交流群,<span style="color: black;">一块</span>学习交流</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「资料」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">免费获取 PHP 等语言学习视频课程。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「文档」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">免费获取 PHP 等语言电子文档</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「激活码」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">免费获取 phpStorm2021版 专属激活码。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">回复「商务合作」</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">就可</span>获取合作方式与客服联系方式。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php是<span style="color: black;">怎样</span>工作的呢?它的生命周期是怎么样的呢?估计有些phper都不<span style="color: black;">晓得</span>吧!下面 都来<span style="color: black;">瞧瞧</span>吧</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">表层分析</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php是以请求/响应为周期运行服务端应用程序,当请求进入应用程序,流程如下: </span><img src="https://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8vSv8JERqUb3M8t0HZ3ibdRxwjgZ6hClaicicEpC3mdQFVqG1ZxxC5azeE8l6sqVcVYs0rzudztF8gaA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"><span style="color: black;">php的每一个请求都会重复以上的交互。</span></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">底层分析</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php的运行模式有两种:web模式和cli模式。无论是哪种运行模式,php的工作原理都是<span style="color: black;">同样</span>的,都是<span style="color: black;">做为</span>一种SAPI运行。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span>,认识下SAPI,它是什么?</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Sapi全<span style="color: black;">叫作</span>是Server Application Programming Interface,<span style="color: black;">亦</span><span style="color: black;">便是</span>服务端应用编程接口,Sapi<span style="color: black;">经过</span>一系列钩子函数,使得PHP<span style="color: black;">能够</span>和外围交互数据,这是PHP非常优雅和成功的一个设计,<span style="color: black;">经过</span>sapi成功的将PHP本身和上层应用</span><span style="color: black;">解耦</span><span style="color: black;">隔离,PHP<span style="color: black;">能够</span><span style="color: black;">再也不</span><span style="color: black;">思虑</span><span style="color: black;">怎样</span>针对<span style="color: black;">区别</span>应用进行兼容,而应用本身<span style="color: black;">亦</span><span style="color: black;">能够</span>针对自己的特点实现<span style="color: black;">区别</span>的处理方式。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">平常</span>的SAPI有cli、cgi、php-fpm以及各服务<span style="color: black;">详细</span>的sapi。 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在php的生命周期中,有4个关键调用: </span><img src="https://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8vSv8JERqUb3M8t0HZ3ibdRxT5C5nzqF3DjBlVbooENBPxo66BWZqVUZgPBLZLiabdaBmBianMU0gLbA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以</span><span style="color: black;">nginx</span><span style="color: black;">服务器</span><span style="color: black;">为例,在web模式下,生命周期流程如下: </span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8vSv8JERqUb3M8t0HZ3ibdRxwUUbA3bHgf7FB6gKYJicTXeHQqc4TWNEQrILr0M08zKDpgP1NvGJiaTQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SAPI运行PHP都经过下面几个<span style="color: black;">周期</span>:</p><strong style="color: blue;">1、模块初始化<span style="color: black;">周期</span>(module init):</strong>这个<span style="color: black;">周期</span><span style="color: black;">重点</span>进行php框架、zend引擎的初始化操作。这个<span style="color: black;">周期</span><span style="color: black;">通常</span>是在SAPI<span style="color: black;">起步</span>时执行一次,<span style="color: black;">针对</span>FPM而言,<span style="color: black;">便是</span>在fpm的master进行<span style="color: black;">起步</span>时执行的。php加载<span style="color: black;">每一个</span>扩展的代码并调用其模块初始化例程(MINIT),进行<span style="color: black;">有些</span>模块所需变量的申请,内存分配等。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、请求初始化<span style="color: black;">周期</span>(request init): </span></strong><span style="color: black;">当一个页面请求<span style="color: black;">出现</span>时,在请求处理前都会经历的一个<span style="color: black;">周期</span>。<span style="color: black;">针对</span>fpm而言,是在worker进程accept一个请求并读取、解析完请求数据后的一个<span style="color: black;">周期</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这个<span style="color: black;">周期</span>内,SAPI层将<span style="color: black;">掌控</span>权交给PHP层,PHP初始化<span style="color: black;">这次</span>请求执行脚本所需的环境变量。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3、php脚本执行<span style="color: black;">周期</span> </span></strong><span style="color: black;">php代码解析执行的过程。Zend引擎接管<span style="color: black;">掌控</span>权,将php脚本代码编译成opcodes并<span style="color: black;">按序</span>执行</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">4、请求结束<span style="color: black;">周期</span>(request shutdown): </span></strong><span style="color: black;">请求处理完后就进入了结束<span style="color: black;">周期</span>,PHP就会<span style="color: black;">起步</span>清理程序。这个<span style="color: black;">周期</span>,将flush输出内容、发送http响应内容等,<span style="color: black;">而后</span>它会按<span style="color: black;">次序</span>调用各个模块的RSHUTDOWN<span style="color: black;">办法</span>。RSHUTDOWN用以清除程序运行时产生的符号表,<span style="color: black;">亦</span><span style="color: black;">便是</span>对<span style="color: black;">每一个</span>变量调用unset函数。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">5、模块关闭<span style="color: black;">周期</span>(module shutdown):</span></strong><span style="color: black;">该<span style="color: black;">周期</span>在SAPI关闭时执行,与模块初始化<span style="color: black;">周期</span>对应,这个<span style="color: black;">周期</span><span style="color: black;">重点</span>是进行资源的清理、php各模块的关闭操作,<span style="color: black;">同期</span>,将回调各扩展的module shutdown钩子函数。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这是<span style="color: black;">出现</span>在所有请求都<span style="color: black;">已然</span>结束之后,例如关闭fpm的操作。(这个是<span style="color: black;">针对</span>CGI和CLI等SAPI,<span style="color: black;">无</span>“下一个请求”,<span style="color: black;">因此</span>SAPI立刻<span style="color: black;">起始</span>关闭。)</span></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">PHP的核心架构图</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8vSv8JERqUb3M8t0HZ3ibdRxzIVd9bKJvrtibeia2grZYJFUYN7QgOvS0I7N4S4ydZDoAiaqKxG4sXVww/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">原文链接:https://www.cnblogs.com/applelife/p/10511837.html</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">点个赞</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/Ljib4So7yuWge7Mibiad1tV0iaF8zSD5gzicbYiaOtl6tmz4onkADe25mdeWcVhZlQ8Q0bH73nZAwICyJ4pPgJU3VrCg/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">再走吧</span></strong></p>
页:
[1]