Почему я получаю эту ошибку:
Обрезаемая фатальная ошибка: объект класса Card не может быть преобразован в строку в /f5/debate/public/Card.php в строке 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 )
Все объявления Card :
public $type; public $tag; public $title; public $source; public $text; public function __construct() { $this->date = new Date; $this->author = new Author; }
После изменения строки 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')";-$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')";-$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')";-$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')";
Теперь я получаю эту ошибку:
Обрезаемая фатальная ошибка: объект класса Автор не может быть преобразован в строку в /f5/debate/public/Card.php в строке 79
Читайте о синтаксическом анализе строк , вы должны заключить в них скобки {} :
$query = "INSERT INTO cards VALUES('$this->type','$this->tag','{$this->author->last}',"
Всякий раз, когда вы хотите получить доступ к многомерным массивам или свойствам свойства в строке, вы должны заключить этот доступ с помощью {} . В противном случае PHP будет анализировать только переменную до первого ->property [i] или ->property .
Таким образом, с "$this->author->last" вместо "{$this->author->last}" , PHP будет анализировать и оценивать $this->author который дает вам ошибку, поскольку author является объектом.
Я не думаю, что вам нужен знак $ при использовании оператора со стрелкой.
вы не должны ставить $ before имена свойств при их доступе:
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); }
Вы пытаетесь эхо-объект сам по себе, а не строковое свойство. Проверьте свой код внимательно.
Вероятно, вы захотите использовать:
$query = "INSERT INTO cards VALUES('$this->type','$this->tag' // etc
Я думаю, что один из объектов не имеет метода toString (), поэтому он не может быть представлен как строка.