<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]php的register_shutdown_function函数详解  ]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Fri, 01 Nov 2013 08:56:37 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：处理网站访问时后面才进行写日志，常看到类似的类里在单例中这样使，好多都是说类中，而没有谈到类中是单例的使用方法，如下：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp; public static function instance($group =&#039;default&#039;)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!isset(self::$_instance[$group]) &#124;&#124;self::$_instance[$group] === NULL) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Create a new instance
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self::$_instance[$group] = new self($group);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Write the logs at shutdown
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register_shutdown_function(array(self::$_instance[$group], &#039;write&#039;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self::$_instance[$group];
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
</textarea><br/><br/>简单实践：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp; &lt;?php
&nbsp;&nbsp;&nbsp;&nbsp;class TestShutdownLog &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private $running;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function main() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;running = true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ob_start();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; error_reporting(E_ALL);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; register_shutdown_function(array($this, &quot;clean_exit&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;注册完shutdown函数&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // triggers E_ERROR
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;running = false;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function clean_exit() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;clean_exit时写在web运行完后在这儿做写日志等的操作,单例模式注册日志先放前面数组，后面在这儿写。&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;?&gt;

&lt;?php
class webStartAndEnd&#123;
&nbsp;&nbsp;&nbsp;&nbsp;function TestShutDown()
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$t = new TestShutdownLog();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$t-&gt;main();//该类注册shutdown函数
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;function dbOrData()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;做数据链接，写入日志内容但不写在shutdown：clean_exit时写。&#92;n&quot;;

&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;function TestAfter()&#123;//做完所有扫尾事宜
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $f = new testafter();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $f-&gt;after();

&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;
?&gt;

&lt;?php
class testafter&#123;
&nbsp;&nbsp;&nbsp;&nbsp;function after()
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;干完所有事情了，立即退出&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&#125;
?&gt;

&lt;?php
$shutDownTest = new webStartAndEnd();
$shutDownTest -&gt;TestShutDown();
$shutDownTest -&gt;dbOrData();
$shutDownTest -&gt;TestAfter();

?&gt;
</textarea><br/><br/>运行情况：<br/>---------- 调试PHP ----------<br/>注册完shutdown函数<br/>做数据链接，写入日志内容但不写在shutdown：clean_exit时写。<br/>干完所有事情了，立即退出<br/>clean_exit时写在web运行完后在这儿做写日志等的操作,单例模式注册日志先放前面数组，后面在这儿写。<br/>输出完成 (耗时 0 秒) - 正常终止<br/><br/>函数讲解：<br/>PHP提供register_shutdown_function()这个函数，能够在脚本终止前回调注册的函数<br/>register_shutdown_function例子代码:<br/><textarea name="code" class="php" rows="15" cols="100">
 &lt;?php
function Test()
&#123;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!file_exists(&#039;Test.txt&#039;))&#123;&nbsp;&nbsp;&nbsp;&nbsp; //判断如果文件不存在!!
echo &#039;文件不存在,我要创建一个:&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$Str = fopen(&#039;Test.txt&#039;,&quot;w+&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fwrite($Str,&#039;you are write after exit&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose($Str);
echo &quot;创建完成!&quot;;
&#125;else &#123;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //如果存在;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;文件已经存在&#039;;
&#125;
&#125;
register_shutdown_function(&#039;Test&#039;);
for($i=0;$i&lt;10;$i++)&#123;
 echo &quot;Echo&lt;br/&gt;&quot;;
&#125;
exit;
?&gt;
</textarea><br/> There is a note &quot;Shutdown function is called during the script shutdown so headers are always already sent.&quot;, but my php 5.1 seems to act differently.<br/>Example:<br/><textarea name="code" class="php" rows="15" cols="100">
 &lt;?php
class Test &#123;
&nbsp;&nbsp; private $running;
&nbsp;&nbsp; public function main() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;running = true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ob_start();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; error_reporting(E_ALL);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; register_shutdown_function(array($this, &quot;clean_exit&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;Hello&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // triggers E_ERROR
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $fatal-&gt;error();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;running = false;&nbsp;&nbsp; 
&nbsp;&nbsp; &#125;
&nbsp;&nbsp; public function clean_exit() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($this-&gt;running) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; header(&quot;Location: error.php&quot;);&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;&nbsp;&nbsp; 
&nbsp;&nbsp; &#125;
&#125;
$t = new Test();
$t-&gt;main();
?&gt;
</textarea><br/>This example redirects you on error.php, this could be a simple way to handle E_ERROR. <br/><br/>来自：http://314200716.blog.163.com/blog/static/55358027201051241028808/<br/>更多兼容参考：<br/>http://www.laruence.com/2008/09/04/498.html
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]php的register_shutdown_function函数详解  ]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>