Adam Hopkinson Adam Hopkinson - 1 month ago 9x
PHP Question

Error passing an instance of an object into a class constructor

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
, it would assign the variable without issue.


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