Site Admin
Site Admin Founder of MeaningArticles

PHP Convert Object to String Example

Hello Dev.

PHP is a programming language which deals with objects and strings pretty frequently, therefore, to Convert a php object into string there is a need of conversion wherein the objects get converted into Strings easily using _toString() function and serialize function. _toString() function in php is extensively used to Convert the object associated with 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 illustration of any value this is stored within the php variable being assumed.

The serialize() method returns a string containing a byte-stream representation of any value that can be stored in PHP.

    class NewObject {
      public $name = 'John Wick';
      public function __toString() {
        return "Movie name is: {$this->name}\n";
    $OBJECT = new NewObject;
    echo $OBJECT;
    echo serialize($OBJECT);

/* Result: 
Movie name is: John Wick
O:9:"NewObject":1:{s:4:"name";s:9:"John Wick";} 

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.