我正尝试通过回叫获得响应。但问题是我无法从回调中获得响应,因为响应为空,而且我不知道如何解决那。如果有人对解决我的问题有任何想法。
我试图从数组中获取数据,并且可以正常工作。
class Deferred
{
const PENDING = 0;
const RESOLVED = 1;
const REJECTED = 2;
private $status = self::PENDING;
//Await require these option for check each state of request.
protected $finished, $notify, $failed;
protected $cancelled = false;
protected $results = [];
/**
* Pool constructor.
* @param callable|null $finished
* @param callable|null $failed
* @param callable|null $notify
*/
public function __construct(?callable $finished = null, ?callable $failed = null, ?callable $notify = null)
{
$this->finished = $finished;
$this->failed = $failed;
$this->notify = $notify;
}
/**
* @return int
*/
public function getStatus(): int
{
return $this->status;
}
/**
* @param $data
* @return $this
*/
public function resolve($data)
{
if ($this->getStatus() === self::PENDING && !$this->isCancelled()) {
if (!is_null($this->finished)) {
call_user_func($this->finished, $data);
}
$this->status = self::RESOLVED;
$this->results = $data;
}
return $this;
}
/**
* @param callable|null $finished
* @param callable|null $failed
* @param callable|null $notify
*/
public function then(?callable $finished = null, ?callable $failed = null, ?callable $notify = null)
{
//response from these states
if ($this->getStatus() != self::PENDING) {
if (!is_null($finished) && $this->getStatus() == self::RESOLVED) call_user_func($finished, $this->results);
if (!is_null($failed) && $this->getStatus() == self::REJECTED) call_user_func($failed, $this->results);
}
if (!is_null($finished)) $this->finished = $finished;
if (!is_null($notify)) $this->notify = $notify;
if (!is_null($failed)) $this->failed = $failed;
}
}
function getData(string $name) : bool{
$pool = new Deferred();
$my = MySQL::getData();
$result = $my->query("SELECT * FROM suggestions WHERE name = '" . $my->real_escape_string(strtolower($name)) . "'");
$response = '';
$pool->then(
function($value) use(&$response) {
$response = $value;
}, function($value){
echo "failed";
});
$pool->resolve($result);
if($response->num_rows > 0) return true;
else return false;
}
var_dump(getData());
“试图获取非对象的属性'num_rows'”