例子代码如下,目的很简单,故意让页面执行超时。期望最后会执行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、程序可以打印出$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);
}
}
这个函数会在所有开启的输出缓存都关闭时调用
谢谢测试但我刷不到10次,就会出现打印不出$this->length;的情况而且,你测试file_put_contents有输出么?
function doSomething()
{
echo $this->length;
echo 'F:' . file_put_contents("justTest.txt", $this->length);
readfile("justTest.txt");
}
每次都有正确的输出
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这时候,文件也就不会写了
1)从命令行运行php thisfile.php,可以输出剩余次数,并且能够将剩余次数写入文件
2)从浏览器运行thisfile.php,可以输出剩余次数,但是不能将剩余次数写入文件(已在类构造函数内加入了写文件操作能够写入文件,可以排除文件权限的问题)以上两点都是必现的结果,不存在有时候可以,有时候不行的情况。
感谢测试的朋友
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
□ □
■ 忍以明志 勤以致远 ■
□ □
■□■□■□■□■□■□■□■
基于CSDN论坛提供的插件扩展功能,自己做了个签名档工具,分享给大家,欢迎技术交流 :)
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.
这个很好用啊~~~~~