8.4.2: htmlspecialchars() expects parameter 1 to be string, object given

Permalink
What can cause a

Whoops \ Exception \ ErrorException (E_WARNING)
htmlspecialchars() expects parameter 1 to be string, object given

I've gone through the whole view and controller with a Log::Info, it always logs to the end of each function. I've deleted the whole view, still whooping. And the Whoops doesn't tell me which package file is causing it.

The only thing which removes the whoop is if I remove setting an entity object variable from the controller in its edit() which the view basically relies on, but I use all other similar variables the same way without problems. And a var_dump_safe of that variable in the view gives me a nice object.

linuxoid
 
JohntheFish replied on at Permalink Reply
JohntheFish
There are so many places that could happen.......

A favourite is with attributes, where the attribute returns a value object as an object or as a string depending on context, and when used in code it often gets the context wrong. In such situations, coercing the parameter to (string) can remove the confusion.

Other than that, you just have to trace through and find the offender. Its one of those annoying things :-(
linuxoid replied on at Permalink Reply
linuxoid
I do not use attributes.

I have ForumPost and ForumReply classes. They are nearly identical, the difference is the replies are related to the post. My page controller works fine with adding/editing the posts. Then I decided I want the same functionality for the replies and basically copied all post functions and renamed them e.g. edit() -> edit_reply(). I set a post class variable in the controller: $this->set('forum_post', $forum_post); - no problem. And I do exactly the same with the reply: $this->set('reply', $reply); .

As I said, trying to get the spot where the error occurs I've deleted the whole code in view it still happens. Then I tried removing chunks of code in the controller and the only thing which clears the error is that bit: $this->set('reply', $reply);. Deleting all code from the function except for this line makes no difference.

That's why I'm puzzled. How on earth does it happen for the reply and all is fine for the post? BTW, the add_reply() works fine because the $reply is null there. AND what else puzzles me is that if I put a Log::Info pass that line, it passes and prints a log message - this means that line does NOT throw the error, it's something somewhere else. But if not there, where else? Where else can I look for what may be causing the error?
linuxoid replied on at Permalink Reply
linuxoid
If I substitute $reply = Reply::getByID($reply_id); with $reply = ForumPost::getByID($forum_post_id);, it throws the error too, although it doesn't do that in the post edit(). And the view is empty! What's wrong with the edit_reply()? How come?

All I have is just this:
public function edit_reply($reply_id = 0, $forum_post_id = 0, $status = '', $error = '')
{
    $reply = Reply::getByID($reply_id);
    if (!$reply) {
        $r = Redirect::to('/dashboard/forum');
        $r->send();
        exit;
    }
    $this->set('reply', $reply);
}

and the URL is:
http://localhost/c584/index.php/dashboard/forum/edit_reply/163/152...

All other functions on the reply in the controller (e.g. approve_reply, delete_reply, flag_reply, save_reply, add_reply etc.) work fine.
linuxoid replied on at Permalink Reply
linuxoid
Well, I got it!

It turns out I have the same variable name set in the post section of the view when I get post replies and loop through them in foreach ($messages as $message), and then I thought I was getting the variable set in the controller while it was set prior to the view and then later overriden by the above.

Damn, I thought I was going crazy! )))

PS. Yet another point - if you think you're going crazy and it should work but it doesn't, good chance it's gotta be the stupidest mistake in code