【发布时间】:2021-03-29 17:51:09
【问题描述】:
我对以下代码有疑问。
<?php
include_once ('vendor/autoload.php');
use Spatie\Async\Pool;
$pool = Pool::create();
for($i = 0; $i < 100; $i++) {
$task = new ExpensiveTask($i);
$pool->add(function () use ($task) {
return $task->delayedProcess();
})->then(function ($output) {
print "Completed task: " . $output;
})->catch(function (Throwable $exception) {
print $exception->getMessage();
})->timeout(function () {
print "timeout";
});
}
$pool->wait();
class ExpensiveTask {
private $taskId;
public function __construct(int $index)
{
$this->taskId = $index;
}
public function delayedProcess() {
sleep(1);
return $this->taskId;
}
}
在执行上述代码时,我收到以下 500 错误。不知道为什么会这样。
PHP Fatal error: {closure}(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "ExpensiveTask" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition in closure://function () use ($task) {
return $task->delayedProcess();
} on line 3
如何解决这个问题,我是 PHP 闭包的新手。即使我尝试在使用它之前声明该类,但得到了同样的错误。
【问题讨论】:
标签: php