Php 对象无法转换为字符串?
为什么我会出现这个错误: 可捕获的致命错误:无法将类别卡的对象转换为 第79行的/f5/discome/public/Card.php中的字符串 代码如下:Php 对象无法转换为字符串?,php,Php,为什么我会出现这个错误: 可捕获的致命错误:无法将类别卡的对象转换为 第79行的/f5/discome/public/Card.php中的字符串 代码如下: public function insert() { $mysql = new DB(debate); $this->initializeInsert(); $query = "INSERT INTO cards VALUES('$this->$type','$this->
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行是$query
,该函数是类卡的一部分
)
卡的所有声明
:
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')";
我现在得到这个错误:
可捕获的致命错误:无法转换类Author的对象
在第79行输入/f5/discome/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);
}
您正在尝试回显对象本身,而不是它的字符串属性。仔细检查您的代码。您可能需要使用:
$query = "INSERT INTO cards VALUES('$this->type','$this->tag' // etc
我认为在使用arrow运算符时不需要$符号。阅读,您必须用括号括住变量。
{}
:
$query = "INSERT INTO cards VALUES('$this->type','$this->tag','{$this->author->last}',"
每当您想要访问多维数组或字符串中属性的属性时,必须用{}
将此访问权括起来。否则,PHP将只解析第一个[i]
或属性->之前的变量
因此,使用
“$this->author->last”
而不是“{$this->author->last}”
,PHP将只解析和计算$this->author
,这会导致错误,因为author
是一个对象。我认为其中一个对象不必字符串()方法的定义使其不能表示为字符串。这是因为$this-$author->qualifications