ReactPHP get_class() 期望参数 1 为对象,给定的字符串

ReactPHP get_class() expects parameter 1 to be object, string given

我收到此错误代码:

PHP Warning: get_class() expects parameter 1 to be object, string given in /phalcon/vendor/clue/block-react/src/functions.php on line 90 PHP Fatal error: Uncaught UnexpectedValueException: Promise rejected with unexpected value of type in /phalcon/vendor/clue/block-react/src/functions.php:89 Stack trace:

  • 0 /phalcon/vendor/clue/block-react/src/functions.php(198): Clue\React\Block\await(NULL, Object(React\EventLoop\StreamSelectLoop), NULL)
  • 1 /phalcon/app/tasks/RunTask.php(96): Clue\React\Block\awaitAll(NULL, Object(React\EventLoop\StreamSelectLoop))

我正在使用以下代码建立我的承诺:

$promises[] = $this->getTrades($rule->id, $exchange->id)
                ->then(
                    function ($trades) use ($handleTrades) {
                        foreach ($trades as $trade) {
                            $handleTrades[] = $trade;
                        }
                    }
                );

函数是这样的:

private function getTrades($rule, $exchange) {
    $deferred = new React\Promise\Deferred();

    //...

    if (empty($trades->count())) {
        $deferred->reject("No trades found for rule $rule");
    } else {
        $deferred->resolve($trades);
    }

    return $deferred->promise();
}

我该如何解决这个问题?

真正的原因是 Fatal error: Uncaught UnexpectedValueException,而不是警告。由于 you can see clue/reactphp-block 库需要 Exception 在您的 reject 函数中。尝试:

$deferred->reject(new \Exception("No trades found for rule $rule"));

正如Furgas上面提到的,我需要在promise中添加错误处理函数。