Why am I getting this error:
为什么我收到此错误:
Catchable fatal error: Object of class Card could not be converted to string in /f5/debate/public/Card.php on line 79
可捕获的致命错误:类卡的对象无法在第79行的/f5/debate/public/Card.php中转换为字符串
Here is the code:
这是代码:
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);
}
(Line 79 is the $query
and the function is part of class Card
)
(第79行是$查询,函数是类卡的一部分)
All the declarations of Card
:
卡的所有声明:
public $type;
public $tag;
public $title;
public $source;
public $text;
public function __construct() {
$this->date = new Date;
$this->author = new Author;
}
After changing line 79 to this:
将第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')";
I now get this error:
我现在得到这个错误:
Catchable fatal error: Object of class Author could not be converted to string in /f5/debate/public/Card.php on line 79
可捕获的致命错误:类79的对象无法在第79行的/f5/debate/public/Card.php中转换为字符串
6 个解决方案
#1
9
Read about string parsing, you have to enclose the variables with brackets {}
:
阅读有关字符串解析的信息,您必须用括号{}括起变量:
$query = "INSERT INTO cards VALUES('$this->type','$this->tag','{$this->author->last}',"
Whenever you want to access multidimensional arrays or properties of a property in string, you have to enclose this access with {}
. Otherwise PHP will only parse the variable up to the first [i]
or ->property
.
无论何时要在字符串中访问多维数组或属性属性,都必须使用{}括起此访问权限。否则PHP只会将变量解析为第一个[i]或 - >属性。
So with "$this->author->last"
instead of "{$this->author->last}"
, PHP will only parse and evaluate $this->author
which gives you the error as author
is an object.
所以使用“$ this-> author-> last”而不是“{$ this-> author-> last}”,PHP将只解析和评估$ this-> author,因为作者是一个对象,它会给你错误。
#2
6
I don't think you need the $ sign when using arrow operator.
使用箭头操作符时,我认为您不需要$符号。
#3
3
you shouldn't put $ before property names when you access them:
在访问属性名称时,不应将$放在属性名称之前:
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);
}
#4
2
You are trying to echo an object itself, not a string property of it. Check your code carefully.
您正在尝试回显对象本身,而不是它的字符串属性。仔细检查您的代码。
#5
2
You probably want to use:
您可能想要使用:
$query = "INSERT INTO cards VALUES('$this->type','$this->tag' // etc
#6
1
I think one of the object doesn't have toString() method defined so it cannot be represented as string.
我认为其中一个对象没有定义toString()方法,所以它不能表示为字符串。
#1
9
Read about string parsing, you have to enclose the variables with brackets {}
:
阅读有关字符串解析的信息,您必须用括号{}括起变量:
$query = "INSERT INTO cards VALUES('$this->type','$this->tag','{$this->author->last}',"
Whenever you want to access multidimensional arrays or properties of a property in string, you have to enclose this access with {}
. Otherwise PHP will only parse the variable up to the first [i]
or ->property
.
无论何时要在字符串中访问多维数组或属性属性,都必须使用{}括起此访问权限。否则PHP只会将变量解析为第一个[i]或 - >属性。
So with "$this->author->last"
instead of "{$this->author->last}"
, PHP will only parse and evaluate $this->author
which gives you the error as author
is an object.
所以使用“$ this-> author-> last”而不是“{$ this-> author-> last}”,PHP将只解析和评估$ this-> author,因为作者是一个对象,它会给你错误。
#2
6
I don't think you need the $ sign when using arrow operator.
使用箭头操作符时,我认为您不需要$符号。
#3
3
you shouldn't put $ before property names when you access them:
在访问属性名称时,不应将$放在属性名称之前:
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);
}
#4
2
You are trying to echo an object itself, not a string property of it. Check your code carefully.
您正在尝试回显对象本身,而不是它的字符串属性。仔细检查您的代码。
#5
2
You probably want to use:
您可能想要使用:
$query = "INSERT INTO cards VALUES('$this->type','$this->tag' // etc
#6
1
I think one of the object doesn't have toString() method defined so it cannot be represented as string.
我认为其中一个对象没有定义toString()方法,所以它不能表示为字符串。