我让我们说2个文件让我们说一个index.php
和另一个included.php
的index.php
<?php
include 'included.php';
while(true)
{
$i++;
sleep(3);
$o = new tester();
}
?>
Run Code Online (Sandbox Code Playgroud)
included.php
<?php
class tester{
public function __construct() {
//execute something
//wait for lets say one minute without stoping the flow of main loop.
//Rest code
}
Run Code Online (Sandbox Code Playgroud)
?>
我想要的是不要停止循环index.php
,当测试者类被激活执行第一部分然后等待我说一分钟?
有可能?还是我需要别的东西?
你不能用PHP做到这一点.它不是一种多线程语言.您可以通过并行执行另一个PHP副本来模糊地模拟它,但这不是真正的线程 - 它是两个完全独立的进程,恰好执行相同的脚本.
您不能在单个PHP实例中同时执行脚本的两个或更多不同部分.