无法将对象转换为字符串? [英] Object could not be converted to string?

查看:96
本文介绍了无法将对象转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现此错误:

可捕获的致命错误:类别为Card的对象无法转换为 第79行的/f5/debate/public/Card.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行是$query,该函数是类Card的一部分)

(Line 79 is the $query and the function is part of class Card)

Card的所有声明:

public $type;

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

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


将第79行更改为此:


After changing line 79 to this:

$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/debate/public/Card.php中输入字符串

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

推荐答案

阅读有关

每当要访问多维数组或字符串中的属性的属性时,都必须使用{}括住此访问权限.否则,PHP只会解析直到 first [i]->property的变量.

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.

因此,对于"$this->author->last"而不是"{$this->author->last}",PHP将仅分析和评估$this->author,这会给您错误,因为author是对象.

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.

这篇关于无法将对象转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆