php – 对象无法转换为字符串?

为什么我得到这个错误:

Catchable fatal error: Object of class Card could not be converted to
string in /f5/debate/public/Card.php on line 79

这是代码:

public function insert()
{
    $mysql = new DB(debate);

    $this->initializeInsert();

    $query = "INSERT INTO cards
            VALUES('$this->$type','$this->$tag','$this->$author->$last','$this->$author->$first','$this->$author->$qualifications','$this->$date->$year','$this->$date->$month','$this->$date->$day','$this->$title','$this->$source','$this->$text')";
            $mysql->execute($query);
}

(79行是$查询,功能是类卡的一部分)

卡的所有声明:

public $type;

public $tag;
public $title;
public $source;
public $text;

public function __construct() {
    $this->date = new Date;
    $this->author = new Author;
}

将79行更改为:

$query = "INSERT INTO cards
    VALUES('$this->type','$this->tag','$this->author->last','$this->author->first','$this-$author->qualifications','$this->date->year','$this->date->month','$this->date->day','$this->title','$this->source','$this->text')";

我现在得到这个错误:

Catchable fatal error: Object of class Author could not be converted
to string in /f5/debate/public/Card.php on line 79

阅读约
string parsing,您必须用括号{}括起变量:

$query = "INSERT INTO cards VALUES('$this->type','{$this->author->last}',"

无论何时要访问字符串中属性的多维数组或属性,都必须使用{}包含此访问.否则,PHP将仅解析变量,直到第一个[i]或 – >属性.

所以使用“$this-> author-> last”而不是“{$this-> author-> last}”,PHP只会解析和评估$this->作者,它将错误提供给作者是一个对象.

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。