Php 将对象实例传递到类构造函数时出错
我在将PSR-7消息响应(由Guzzle生成)传递到类构造函数时遇到问题 该消息由以下程序生成:Php 将对象实例传递到类构造函数时出错,php,oop,guzzle,psr-7,Php,Oop,Guzzle,Psr 7,我在将PSR-7消息响应(由Guzzle生成)传递到类构造函数时遇到问题 该消息由以下程序生成: $client = new \GuzzleHttp\Client(); $res = $client->request('GET', 'http://pagecrawler/cache.html'); 和我的类构造函数: Class Test { protected $response; public function __construct($response, $db
$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
可以将响应对象强制转换为字符串。相反,你应该做$this->response
这只是一个打字错误。使用$this->$response
可以将响应对象强制转换为字符串。相反,您应该执行$this->response
您看到了吗?$this->response,而不是$this->$response@Federico谢谢,就这样!愚蠢的错误-我的所有其他变量设置($this->db=$db
等)都是正确的。是否愿意将其添加为我可以接受的答案?您看到了吗?$this->response,而不是$this->$response@Federico谢谢,就这样!愚蠢的错误-我的所有其他变量设置($this->db=$db
等)都是正确的。要不要加上它作为答案,这样我就可以接受了?