Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a PHP object to a string? [duplicate]

Tags:

php

Possible Duplicate:
PHP ToString() equivalent

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 :-)

like image 819
Rohan Avatar asked Mar 29 '10 12:03

Rohan


People also ask

How to convert PHP object to string?

_toString() function in PHP is extensively used to convert the object related to it into string whereas the serialize() function is used to convert the object and return a string containing entire byte-stream which at later point of time is used for representation of any value that is stored in the PHP variable being ...

How to duplicate an object in PHP?

An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). $copy_of_object = clone $object; When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references.

How do you turn an object into a string?

We can convert Object to String in java using toString() method of Object class or String. valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else.


1 Answers

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.

like image 164
Wim Avatar answered Sep 17 '22 13:09

Wim