对象无法转换为字符串?

cac*_*bin 15 php

为什么我收到此错误:

可捕获的致命错误:类卡的对象无法在第79行的/f5/debate/public/Card.php中转换为字符串

这是代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

(第79行是$query,函数是类的一部分Card)

所有声明Card:

public $type;

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

public function __construct() {
    $this->date = new Date;
    $this->author = new Author;
}
Run Code Online (Sandbox Code Playgroud)

将第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')";
Run Code Online (Sandbox Code Playgroud)

我现在得到这个错误:

可捕获的致命错误:类79的对象无法在第79行的/f5/debate/public/Card.php中转换为字符串

Fel*_*ing 10

阅读有关字符串解析的信息,您必须用括号括起变量{}:

$query = "INSERT INTO cards VALUES('$this->type','$this->tag','{$this->author->last}',"
Run Code Online (Sandbox Code Playgroud)

无论何时想要在字符串中访问多维数组或属性属性,都必须用此包含此访问权限{}.否则PHP只会将变量解析为第一个 [i]->property.

因此,"$this->author->last"代替"{$this->author->last}",PHP将只解析和评估$this->author哪个给出错误,因为它author是一个对象.


uvg*_*ovy 6

使用箭头操作符时,我认为您不需要$符号.