Доступ к частной собственности в PHP объекте - PullRequest
0 голосов
/ 16 февраля 2020

Я пытался получить доступ к коду статуса и фразе причины, но приведенный ниже код возвращает ошибки типа

die(var_dump($registrant->response->reasonPhrase));

ошибка:

"message": "Не определено свойство: MacsiDigital \ Zoom \ Поддержка \ Response :: $ reasonPhrase ",

код: (РЕЗУЛЬТАТ var_dump ($ registrant-> getResponse ());)

 object(MacsiDigital\Zoom\Support\Response)#874 (1) {
  ["response":protected]=>
  object(GuzzleHttp\Psr7\Response)#885 (6) {
    ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
    string(7) "Created"
    ["statusCode":"GuzzleHttp\Psr7\Response":private]=>
    int(201)
    ["headers":"GuzzleHttp\Psr7\Response":private]=>
    array(14) {
      ["Date"]=>
      array(1) {
        [0]=>
        string(29) "Sun, 16 Feb 2020 05:56:11 GMT"
      }
      ["Content-Type"]=>
      array(1) {
        [0]=>
        string(30) "application/json;charset=UTF-8"
      }
      ["Content-Length"]=>
      array(1) {
        [0]=>
        string(3) "249"
      }
      ["Connection"]=>
      array(1) {
        [0]=>
        string(10) "keep-alive"
      }
      ["Server"]=>
      array(1) {
        [0]=>
        string(4) "ZOOM"
      }
      ["x-zm-trackingid"]=>
      array(1) {
        [0]=>
        string(36) ""
      }
      ["X-Content-Type-Options"]=>
      array(1) {
        [0]=>
        string(7) "nosniff"
      }
      ["Cache-Control"]=>
      array(1) {
        [0]=>
        string(49) "no-cache, no-store, must-revalidate, no-transform"
      }
      ["Pragma"]=>
      array(1) {
        [0]=>
        string(8) "no-cache"
      }
      ["Expires"]=>
      array(1) {
        [0]=>
        string(29) "Thu, 01 Jan 1970 00:00:00 GMT"
      }
      ["Set-Cookie"]=>
      array(1) {
        [0]=>
        string(63) "cred=; Path=/; Secure; HttpOnly"
      }
      ["Strict-Transport-Security"]=>
      array(1) {
        [0]=>
        string(16) "max-age=31536000"
      }
      ["X-XSS-Protection"]=>
      array(1) {
        [0]=>
        string(13) "1; mode=block"
      }
      ["Referrer-Policy"]=>
      array(1) {
        [0]=>
        string(31) "strict-origin-when-cross-origin"
      }
    }
    ["headerNames":"GuzzleHttp\Psr7\Response":private]=>
    array(14) {
      ["date"]=>
      string(4) "Date"
      ["content-type"]=>
      string(12) "Content-Type"
      ["content-length"]=>
      string(14) "Content-Length"
      ["connection"]=>
      string(10) "Connection"
      ["server"]=>
      string(6) "Server"
      ["x-zm-trackingid"]=>
      string(15) "x-zm-trackingid"
      ["x-content-type-options"]=>
      string(22) "X-Content-Type-Options"
      ["cache-control"]=>
      string(13) "Cache-Control"
      ["pragma"]=>
      string(6) "Pragma"
      ["expires"]=>
      string(7) "Expires"
      ["set-cookie"]=>
      string(10) "Set-Cookie"
      ["strict-transport-security"]=>
      string(25) "Strict-Transport-Security"
      ["x-xss-protection"]=>
      string(16) "X-XSS-Protection"
      ["referrer-policy"]=>
      string(15) "Referrer-Policy"
    }
    ["protocol":"GuzzleHttp\Psr7\Response":private]=>
    string(3) "1.1"
    ["stream":"GuzzleHttp\Psr7\Response":private]=>
    object(GuzzleHttp\Psr7\Stream)#883 (7) {
      ["stream":"GuzzleHttp\Psr7\Stream":private]=>
      resource(650) of type (stream)
      ["size":"GuzzleHttp\Psr7\Stream":private]=>
      NULL
      ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["readable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["writable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["uri":"GuzzleHttp\Psr7\Stream":private]=>
      string(10) "php://temp"
      ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
      array(0) {
      }
    }
  }
}

PS. : когда я пытаюсь получить ie такой доступ, возвращается Null

$registrant->reasonPhrase

Ответы [ 2 ]

1 голос
/ 16 февраля 2020

Ну, в PHP вы не можете получить доступ к частному свойству вне класса. Вы можете получить доступ к protected свойству в подклассах класса. Но если они необходимы для доступа клиентов / вас (имеется в виду, кто использует объект / код), разработчики такого типа классов используют getters и setters метод для доступа к ним. Так что вы можете использовать их следующим образом, если они вам нужны.

$registrant->getResponse()->getResponse()->getReasonPhrase();

На самом деле этот метод взят из класса GuzzleHttp\Psr7\Response этого Guzzle. Если вы хотите, вы можете найти его здесь

0 голосов
/ 16 февраля 2020

Для проверки личных свойств вашего MacsiDigital\Zoom\Support\Response Obj у вас нет другого выбора, кроме как использовать методы установки / получения.

Попробуйте

dump($registrant->getResponse());

Тогда

dump(($registrant->getResponse())->getReasonPhrase());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...