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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3200899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:59:17  来源:igfitidea点击:

Object could not be converted to string?

php

提问by cactusbin

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 行的 Card 类对象无法转换为 /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 $queryand the function is part of class Card)

(第 79 行是$query并且函数是 class 的一部分Card

All the declarations of Card:

的所有声明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 行的 /f5/debate/public/Card.php 中将类 Author 的对象转换为字符串

采纳答案by Felix Kling

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]->property.

So with "$this->author->last"instead of "{$this->author->last}", PHP will only parse and evaluate $this->authorwhich gives you the error as authoris an object.

因此,使用"$this->author->last"代替"{$this->author->last}",PHP 只会解析和评估$this->author哪个会像author对象一样给出错误。

回答by uvgroovy

I don't think you need the $ sign when using arrow operator.

我认为使用箭头运算符时不需要 $ 符号。

回答by Sergey Eremin

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);
    }

回答by Jacob Relkin

You are trying to echo an object itself, not a string property of it. Check your code carefully.

您正在尝试回显对象本身,而不是它的字符串属性。仔细检查您的代码。

回答by quantumSoup

You probably want to use:

您可能想要使用:

$query = "INSERT INTO cards VALUES('$this->type','$this->tag' // etc

回答by Alex Kleshchevnikov

I think one of the object doesn't have toString() method defined so it cannot be represented as string.

我认为其中一个对象没有定义 toString() 方法,因此它不能表示为字符串。