0

I'm having an issue passing a PSR-7 message response (generated by Guzzle) into a class constructor.

The message is generated by:

$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'http://pagecrawler/cache.html');

And my class constructor:

Class Test {

    protected $response;

    public function __construct($response, $db = null)
    {
        $this->$response = $response; /* Line 18 */
    }
}

The error I'm getting is:

PHP Catchable fatal error:  Object of class GuzzleHttp\Psr7\Response could not be converted to string

I'd assumed that, as I'm not setting a type for $this->response, it would assign the variable without issue.

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • Have you seen http://stackoverflow.com/questions/30549226/guzzlehttp-how-get-the-body-of-a-response-from-guzzle-6 ? – Blake Sep 07 '16 at 19:32
  • 3
    $this->response, not $this->$response – Federkun Sep 07 '16 at 19:35
  • @Federico thank you, that was it! Stupid mistake - all my other variable setups (`$this->db = $db`, etc) were correct. Care to add it as an answer so I can accept? – Adam Hopkinson Sep 07 '16 at 19:58

1 Answers1

2

It's just a typo. With $this->$response you cast the Response object to a string. Instead you should do $this->response.

Federkun
  • 36,084
  • 8
  • 78
  • 90