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

前端之家收集整理的这篇文章主要介绍了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->作者,它将错误提供给作者是一个对象.

猜你在找的PHP相关文章