Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
了解PHP脚本何时开始执行_Php_Oop_Debugging - Fatal编程技术网

了解PHP脚本何时开始执行

了解PHP脚本何时开始执行,php,oop,debugging,Php,Oop,Debugging,如果要计算特定PHP脚本已运行的时间,请按以下步骤执行: //Remember beginning time of the script define("START_TIME", time()); //Do some loop or whatever ... //At the end, calculate the difference of start and current time echo "This script has been running for ".(time() -

如果要计算特定PHP脚本已运行的时间,请按以下步骤执行:

//Remember beginning time of the script
define("START_TIME", time());

//Do some loop or whatever
   ...

//At the end, calculate the difference of start and current time
echo "This script has been running for ".(time() - START_TIME)."\n";
好的,这很好,但是假设您正在创建一个类/库,它可能会包含在脚本的后面,并且还需要计算运行时

PHP中是否有一个函数可以在脚本启动时给我时间戳,或者有一个函数可以让我扣除时间戳?
这就是我需要的:

class Blah {
    public getRuntime() {
        static $startTime = get_start_time_magic_function();
        return time() - $startTime;
    }
}
我之所以要求这样做,是因为我在一个类中有一个“无限”循环,当执行限制生效时,它应该自动结束。如果脚本被PHP杀死,则会对已处理的数据造成损害。

define(“START_TIME”,TIME());
定义(“最大持续时间”,3);
while(true){
$current\u duration=time()-开始时间;
//在这里做你的事
如果($当前持续时间>=最大持续时间){
echo“此脚本已运行了“.$current\u duration.”秒\n;
打破
}
}
引用(示例3):



@juanrpozo我以前已经读过这篇文章了。或者(从PHP5.4.0开始)
$\u服务器['REQUEST\u TIME\u FLOAT']
怎么样?虽然从技术上讲,这些可能不受脚本启动时间戳本身的影响,我认为对于大多数用例来说,这个值应该足够接近。但是我不确定您试图解决的实际问题是否应该通过查看执行时间/开始时间戳来解决……特别是因为您说由于最大执行时间而终止的脚本将“对处理的数据造成伤害”–我认为这不是检查“我还有更多时间吗?”的情况,而是重新编写脚本以实现更健壮的数据处理。我可以问一下,您认为什么是更健壮的数据处理?这就是我现在所做的。我的问题是如何避免定义开始时间。在用无限循环调用类的脚本中定义它,并将其作为参数传递。或者把循环必须作为参数退出的时间。现在我这样做,但是在我的问题中,我已经陈述了为什么我认为这是不准确的。
<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));

// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

echo "Did nothing in $time seconds\n";
?>