tags:

views:

164

answers:

6

I get this error:

Catchable fatal error: Object of class stdClass could not be converted to string

So, my question is, how do I convert an object to a string in PHP? I don't want to serialize it though.

Just a note: the code I use works in PHP 4, but not in PHP 5

Thanks!

EDIT: I resolved it myself. It was a pain, but I did it. Thanks anyway, everyone :-)

+3  A: 

Why do you need this string? If you just need to visualize it for debugging, you can use var_dump(), print_r(), or $s = print_r($var, 1); to really make it into a string for further theming. If you need to send the object as text to somewhere else (database, Javascript code), you have a choice of serialize, json_encode, XML conversion, and many more, depending on your exact situation.

Wim
+1  A: 

I suspect that this is not a problem with the object not convertable to a string.

It's more like that you are assuming that a variable contains a string, (or something that can represented as a string), but it contains an object, and you are trying to echo that out.

WishCow
A: 

from the error you get I assume you used the object as a parameter to a function that expects a string. the question is, why you did that and what you expect that function to with the object. and then before passing it to the function, extract a sensible string value.

greetz

back2dos

back2dos
+2  A: 

What you need is to add the magic method __toString to your class so you can print what you want.

Edit: Sorry, just saw "PHP 4, but not in PHP 5".

p4bl0
+1  A: 

you can use php magic method __toString

solomongaby
You cannot implement __toString on StdClass.
WishCow
A: 

You do not try to convert this Object in string,it will not work. Just fetch exact value from that Object.

As a example if you got output as cost.this cost output is Object. so fetch exact value from this as follows

  $var=cost->Postage_cost;
  $var1=cost->other_cost;
MAS1