将对象的实例传递到类构造函数时出错


Error passing an instance of an object into a class constructor

我在将PSR-7消息响应(由Guzzle生成(传递到类构造函数时遇到问题。

该消息由以下人员生成:

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

我的类构造函数:

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

我得到的错误是:

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

我假设,由于我没有为$this->response设置类型,它会毫无问题地分配变量。

这只是一个拼写错误。使用$this->$response可以将Response对象强制转换为字符串。相反,您应该执行$this->response