例子代码如下,目的很简单,故意让页面执行超时。期望最后会执行doSomething方法问题如下:
   1、程序可以打印出$this->length,但是不是100%,当失败的时候提示echo $this->length;行超时
   2、file_put_contents从没执行成功过,为什么呢?
   3、我期望程序每次最后都能执行doSomething【这里不用__destruct】,有什么办法呢?error_reporting(E_ALL);
set_time_limit(rand(1,3));$tt = new TT(10);
$tt->TODO();class TT
{
private $length = 0;
function __construct($length = 0)
{
$this->length = $length;
register_shutdown_function(array(&$this, 'doSomething'));
}
function TODO()
{
while($this->length--)
{
sleep(1);
}
}
function doSomething()
{
echo $this->length;
file_put_contents("justTest.txt", $this->length);
}
}

解决方案 »

  1.   

    本帖最后由 xuzuning 于 2010-09-07 09:42:24 编辑
      

  2.   

    register_shutdown_function 注册函数体当脚本进度完成(执行完成)或者调用exit()退出时将被执行
    这个函数会在所有开启的输出缓存都关闭时调用
      

  3.   


    谢谢测试但我刷不到10次,就会出现打印不出$this->length;的情况而且,你测试file_put_contents有输出么?
      

  4.   

    是的,我加了两句
       function doSomething()
        {
            echo    $this->length;
            echo 'F:' . file_put_contents("justTest.txt", $this->length);
            readfile("justTest.txt");
        }
    每次都有正确的输出
      

  5.   

    文件的确是可写出来,,,
    Fatal error: Maximum execution time of 2 seconds exceeded in F:\www\sess.php on line 21Fatal error: Maximum execution time of 2 seconds exceeded in F:\www\sess.php on line 25类似出现这样的提示,第二行就是输出 echo $this->length;的情况,还是会出现。换句话说调用但没执行完毕doSomething这时候,文件也就不会写了
      

  6.   

    我是这么理解的 发生fatal的话 很多php的正常流程逻辑都不会继续执行了应该通过异常处理来搞这个问题吧
      

  7.   

    我的测试结果是:
    1)从命令行运行php thisfile.php,可以输出剩余次数,并且能够将剩余次数写入文件
    2)从浏览器运行thisfile.php,可以输出剩余次数,但是不能将剩余次数写入文件(已在类构造函数内加入了写文件操作能够写入文件,可以排除文件权限的问题)以上两点都是必现的结果,不存在有时候可以,有时候不行的情况。
      

  8.   

    TO CunningBoy我忘记说,文件能写,是file_get_contents(dirname(__FILE__) . "/justTest.txt");给绝对路径,我可以写出来而xuzuning说的,我也测试了,的确是能读出来的,,但是目录下找不到这个文件------------------------------------------------------------------------------
    感谢测试的朋友
      

  9.   

    执行了。错误好多啊:
    Fatal error: Maximum execution time of 1 second exceeded in E:\phpxuexi\client.php on line 21
    9
    Warning: file_put_contents() [function.file-put-contents]: open_basedir restriction in effect. File(justTest.txt) is not within the allowed path(s): (e:/phpxuexi) in E:\phpxuexi\client.php on line 26Warning: file_put_contents(justTest.txt) [function.file-put-contents]: failed to open stream: Operation not permitted in E:\phpxuexi\client.php on line 26
    F:
    Warning: readfile() [function.readfile]: open_basedir restriction in effect. File(justTest.txt) is not within the allowed path(s): (e:/phpxuexi) in E:\phpxuexi\client.php on line 27Warning: readfile(justTest.txt) [function.readfile]: failed to open stream: Operation not permitted in E:\phpxuexi\client.php on line 27
      

  10.   

    TO    falizixun2     你的php.ini配置有问题,,你可以改变php.ini的open_basedir试试
      

  11.   

    我试验的结果更奇怪!从来没有发生“超时”的错误,其它的程序逻辑都是按照“预期”的方式运行的。我甚至在 TODO() 里构造了一个死循环 while(true); 结果它就一直在跑(我的 CPU 是 4 核的,结果负载就一直是 25%),我都等了 5 分钟了,还没出来呢(PHP 5.2.12 - win32)■□■□■□■□■□■□■□■
    □             □
    ■  忍以明志 勤以致远  ■
    □             □
    ■□■□■□■□■□■□■□■
    基于CSDN论坛提供的插件扩展功能,自己做了个签名档工具,分享给大家,欢迎技术交流 :)
      

  12.   

    另外对楼主那个 callback 的写法比较感兴趣:
    register_shutdown_function(array(&$this, 'doSomething'));
    我以前都是用类似下面这样的写法,好像效果应该一样吧?
    register_shutdown_function(array($this, 'doSomething'));
    --------
    With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead.
      

  13.   

    register_shutdown_function
    这个很好用啊~~~~~
      

  14.   

    Fatal error: Maximum execution time of 1 second exceeded in E:\workspace\date\wenjian\t.php on line 23Fatal error: Maximum execution time of 1 second exceeded in E:\workspace\date\wenjian\t.php on line 29
      

  15.   

    Fatal error: Maximum execution time of 3 seconds exceeded in