用PHP这么久了,有些人还不晓得它是怎么样工作的
<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;">微X</span>:2230304070 </strong></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><a style="color: black;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">PHP技术交流<span style="color: black;">微X</span>群 </span></strong></span></a></span></p><img src="https://mmbiz.qpic.cn/mmbiz_png/bL2iaicTYdZn5RYeZyo5amnOAgOiaRia5pTmJkLwBXH0J3icajPObPVuj8C5TeYLc8fI9KZickXDnsa1EvhSibltJv7VQ/640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1&tp=webp" style="width: 50%; margin-bottom: 20px;"><strong style="color: blue;"><span style="color: black;">全网最新phpstorm正式版激活码,支持一键激活多个设备</span></strong><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">链接:</span><span style="color: black;">https://www.mano100.cn/thread-1942-1-1.html</span></span></strong></span>
<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&wxfrom=5&wx_lazy=1&wx_co=1&tp=webp" 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;"><strong style="color: blue;"><span style="color: black;">在php的生命周期中,有4个关键调用: </span></strong><img src="https://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8vSv8JERqUb3M8t0HZ3ibdRxT5C5nzqF3DjBlVbooENBPxo66BWZqVUZgPBLZLiabdaBmBianMU0gLbA/640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1&tp=webp" 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&wxfrom=5&wx_lazy=1&wx_co=1&tp=webp" 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&wxfrom=5&wx_lazy=1&wx_co=1&tp=webp" style="width: 50%; margin-bottom: 20px;"></p><img src="https://mmbiz.qpic.cn/mmbiz_gif/X36HLl2EicOfbCTTtzwpbdicOhvibmKu9O9jGSLPicT5IVjk2U8YETibumKAAa5K55Y3mdvGOvhjfbUGdEibKx8E5BJQ/640?wx_fmt=gif&wxfrom=5&wx_lazy=1&tp=webp" style="width: 50%; margin-bottom: 20px;"><strong style="color: blue;"><span style="color: black;">参考链接:https://www.cnblogs.com/applelife/p/10511837.html</span></strong><span style="color: black;"><span style="color: black;">以上<span style="color: black;">便是</span>本篇分钟的<span style="color: black;">所有</span>内容,</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">期盼</span>各位程序员们<span style="color: black;">奋斗</span><span style="color: black;">提高</span>个人技术。</span></strong></span><span style="color: black;">最后,<span style="color: black;">博主</span>温馨提示:<span style="color: black;">每日</span>阅读5分钟,<span style="color: black;">每日</span>学习一点点,<span style="color: black;">每日</span>进步一点点。</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/X36HLl2EicOfSLeDjYdhheic53iaMxy6jzuQaBEXzfu12r8KsocY8g1D5GGfcUJ4nibr37JY3ZZBu1wjKiaNxEASXUA/640?wx_fmt=gif&wxfrom=5&wx_lazy=1&tp=webp" style="width: 50%; margin-bottom: 20px;"></p>
对于这个问题,我有不同的看法...
页:
[1]