Object could not be converted to string?

后端 未结 6 808
逝去的感伤
逝去的感伤 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: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.

提交回复
热议问题