Convert an Object to a String in PHP

18,892

Solution 1

Maybe serializing? It will take an object/array and convert it to a string (which can then be un-serialized back later)

Solution 2

json_encode and json_decode will also accomplish many of the properties you are looking for via serialize. The advantage is that you can send JSON-encoded data to a web browser and JavaScript can view and modify properties like a native JavaScript object. In addition, JSON is lighter weight than serialized data because its syntax is a lot more compact.

Share:
18,892
Allan
Author by

Allan

___ ,o88888 ,o8888888' ,:o:o:oooo. ,8O88Pd8888" ,.::.::o:ooooOoOoO. ,oO8O8Pd888'" ,.:.::o:ooOoOoOO8O8OOo.8OOPd8O8O" , ..:.::o:ooOoOOOO8OOOOo.FdO8O8" , ..:.::o:ooOoOO8O888O8O,COCOO" , . ..:.::o:ooOoOOOO8OOOOCOCO" . ..:.::o:ooOoOoOO8O8OCCCC"o . ..:.::o:ooooOoCoCCC"o:o . ..:.::o:o:,cooooCo"oo:o: ` . . ..:.:cocoooo"'o:o:::' .` . ..::ccccoc"'o:o:o:::' :.:. ,c:cccc"':.:.:.:.:.' ..:.:"'`::::c:"'..:.:.:.:.:.' ...:.'.:.::::"' . . . . .' .. . ....:."' ` . . . '' . . . ...."' .. . ."' .

Updated on June 04, 2022

Comments

  • Allan
    Allan almost 2 years

    I want to convert an Object into a String in PHP. Specifically, I'm trying to take a mysql query response, and I'm trying to convert it into something I can write to a file and use later.

    Of course, when you try to write an Object to a file, PHP appropriately yells: Catchable fatal error: Object of class DB_result could not be converted to string in .....

    Alternatively, if there is some other way of writing the result of a mysql query to a file, that works too. I'm playing around with a home-brewed caching project :)