Heim  >  Artikel  >  php教程  >  fatal error:php中通过register_shutdown_function记录fatal error

fatal error:php中通过register_shutdown_function记录fatal error

WBOY
WBOYOriginal
2016-06-21 08:50:111215Durchsuche

今天发现php中,如果要记录fatal error的时候,可以实用一个不错的函数,
叫register_shutdown_function,小结如下:
  register_shutdown_function可以让我们设置一个当执行关闭时可以被调用的另一个函数.也就是说当我们的脚本执行完成或意外死掉 导致PHP执行即将关闭时,我们的这个函数将会被调用.所以,我们可以使用在脚本开始处设置一个变量为false,然后在脚本末尾将之设置为true的方 法,让PHP关闭回调函数检查脚本完成与否. 如果我们的变量仍旧是false,我们就知道脚本的最后一行没有执行,因此它肯定在程序执行到某处死掉了;
   例子如下:
 

<ol class="dp-c">
<li class="alt"><span><span>?php </span></span></li>
<li><span><span class="vars">$clean</span><span> = false; </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> shutdown_func(){ </span></span></li>
<li><span><span class="keyword">global</span><span> </span><span class="vars">$clean</span><span>; </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span> (!</span><span class="vars">$clean</span><span>){ </span></span></li>
<li><span><span class="keyword">die</span><span>(</span><span class="string">"not a clean shutdown"</span><span>); </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="keyword">return</span><span> false; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span>register_shutdown_function(<span class="string">"shutdown_func"</span><span>); </span></span></li>
<li class="alt"><span><span class="vars">$a</span><span> = 1; </span></span></li>
<li><span><span class="vars">$a</span><span> = </span><span class="keyword">new</span><span> FooClass(); </span><span class="comment">// 将因为致命错误而失败</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$clean</span><span> = true; </span></span></li>
<li><span>?> </span></li>
<li class="alt"><span>再来一个记录fatal error的例子 </span></li>
<li><span><span class="keyword">function</span><span> catch_fatal_error() </span></span></li>
<li class="alt"><span>{ </span></li>
<li><span><span class="vars">$last_error</span><span> = error_get_last(); </span></span></li>
<li class="alt"><span><span class="comment">// 检查是否属于fatal_error</span><span> </span></span></li>
<li><span><span class="keyword">if</span><span>(isset(</span><span class="vars">$last_error</span><span>[</span><span class="string">'type'</span><span>]) &&</span><span class="vars">$last_error</span><span>[</span><span class="string">'type'</span><span>]==E_ERROR) </span></span></li>
<li class="alt"><span>{ </span></li>
<li><span><span class="comment">//相关的处理</span><span> </span></span></li>
<li class="alt"><span>} </span></li>
<li><span>} </span></li>
<li class="alt"><span>register_shutdown_function(<span class="string">'catch_fatal_error'</span><span>); </span></span></li>
</ol>


  注意的是;
1,register_shutdown_function()函数可重复调用,但执行的顺序与注册的顺序相同
2,如果在调用register_shutdown_function()函数之前有exit()函数调用,register_shutdown_function()函数将不能执行
3,PHP4后支持注册函数参数传递
4,在某些服务端,如Apache,当前目录在register_shutdown_function()函数中能够改变
5,register_shutdown_function()函数执行在headers发送之后

本文链接http://www.cxybl.com/html/wlbc/Php/20130318/37221.html



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn