4

I am completely lost here... After validating some input I create an instance of a Message class and attempt to insert data into the database:

// send message
$message = $this->model->build('message', true);
$message->insertMessage($uid, $user->user_id, $title, $message);

The method for insertion is very straight-forward:

// insert a new message
public function insertMessage($to_id, $from_id, $title, $body)
{
    $sql = "INSERT INTO messages (to_id, from_id, title, body, create_date) VALUES
                                 (:to_id, :from_id, :title, :body, NOW())";
    $sth = $this->db->prepare($sql);
    return $sth->execute([':to_id' => $to_id, ':from_id' => $from_id, ':title' => $title, ':body' => $body]);
}

However, upon submission I end up with a blank page and the Apache error log says:

[Tue Jul 30 22:34:44 2013] [error] [client 127.0.0.1] PHP Catchable fatal error: Object of class framework\models\Message could not be converted to string in /var/www/p-lug/p-lug_lib/framework/models/Message.php on line 18, referer: https://p-lug.localhost/message/compose/4

Line 18 refers to the return statement, but even if I remove return it results in the same error.

I've read countless links regarding this error but none of the answers appear to apply to my example. At no point am I trying to convert an object to a string or output the result, and similar code for insertions with other classes works perfectly. In fact, this code was copy-pasted from another working example, the only thing changed is the table and data.

I've used var_dump() on all the variables being passed, on $this and $sth, everything checks out. But on execute() it fails. What the heck is going on here?

2
  • What does Message class have? Could you post that?
    – vee
    Commented Jul 31, 2013 at 4:01
  • but even if I remove return it results in the same error. that means the error is on the line above. Check if $this->db is valid.
    – Raptor
    Commented Jul 31, 2013 at 4:02

2 Answers 2

15

So $message contains an object.

This object gets passed to the function insertMessage as the 4th argument ($body which is still the same object)

You then store the Message object stored in the variable $body in the hash array which is passed as an argument to execute.

The execute function attempts to convert the Message object to a string but finds that there is not __toString function declared.

So either declare

public function __toString() {
    return $the_string;
}

or create another public function/member that you can pass to the execute function.

2
  • You're right. I defined $message from _POST, and then inadvertently redefined it when creating the object and passed it to itself. Not the first time I've mixed up variables like that either, damn it. Thank you! Commented Jul 31, 2013 at 4:13
  • Right that is exactly what I meant to say lol. I thought that passing the objects reference to itself (more or less) was kind of weird. :P Commented Jul 31, 2013 at 13:55
0

$message which is passed to the $body argument is an object returned from your build() method in the model.

You can simply return the message that was built in the build() method rather than an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.