Ben
Ben

Reputation: 62444

isset() and property_exists() not detecting property

While I'd like to look at an http status code for this, I don't have access to one. So I'm looking at the response class trying to determine if a property exists and having some trouble. This is PHP 7.

private function wasRateLimited(Result $result) : bool
{
    var_dump($result, isset($result->retry_after), property_exists($result, 'retry_after'));

    return isset($result->retry_after);
}

Here's the output:

/Users/myuser/mysite/app/Discord/MessageSender.php:95:
class GuzzleHttp\Command\Result#1153 (3) {
  public $global =>
  bool(false)
  public $message =>
  string(27) "You are being rate limited."
  public $retry_after =>
  int(3615)
}
/Users/myuser/mysite/app/Discord/MessageSender.php:95:
bool(false)
/Users/myuser/mysite/app/Discord/MessageSender.php:95:
bool(false)

Why am I not able to determine if this property exists on the response?

Upvotes: 0

Views: 202

Answers (2)

Scuzzy
Scuzzy

Reputation: 12332

Because Guzzle class Result implements ResultInterface which extends ArrayAccess you'll have to use $result['retry_after'] to get your property.

var_dump( $result['retry_after'] );

var_dump( isset( $result['retry_after'] ) );

Upvotes: 1

aidinMC
aidinMC

Reputation: 1436

Try this:

private function wasRateLimited(Result $result) : bool
{
    var_dump($result, isset($result->retry_after), property_exists($result, 'retry_after'));

    return ($result->retry_after) ? true : false;
}

Upvotes: 0

Related Questions