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

Error passing an instance of an object into a class constructor

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

消息生成者:

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

还有我的 class 构造函数:

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.