Object could not be converted to string?

后端 未结 6 772
逝去的感伤
逝去的感伤 2020-12-10 11:28

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

相关标签:
6条回答
  • 2020-12-10 12:02

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

    0 讨论(0)
  • 2020-12-10 12:08

    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.

    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.

    0 讨论(0)
  • 2020-12-10 12:08

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

    0 讨论(0)
  • 2020-12-10 12:10

    You probably want to use:

    $query = "INSERT INTO cards VALUES('$this->type','$this->tag' // etc
    
    0 讨论(0)
  • 2020-12-10 12:20

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

    0 讨论(0)
  • 2020-12-10 12:24

    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);
        }
    
    0 讨论(0)
提交回复
热议问题