Using the Print_r() Function
The first techique we can use to convert an object to a string is the print_r function. The function takes the value to be printed and a return parameter. The return parameter is a boolean value.
For example, to convert an object to a string using the print_r function, we can provide the object as the first parameter (value to be printed on the screen) and a boolean true as the second parameter.
The syntax of the function is as shown:
By default, the boolean value is set to false. If not provided, the print_r function will print the value of the variable provided.
Let us take a simple example. We will create a simple Car class with two variables: the name and the price. We will create a constructor with the name and price as the parameters and the variables.
Finally, we create an object from the car class and give them sample data for the name and price. To convert the object to a string, we will use the print_r function as shown in the example below:
class Car {
protected $name;
protected $price;
public function __construct($name, $price) {
$this->name =$name;
$this->price =$price;
}
}
$mycar = new Car("Ford", 25000);
$obj_str = print_r($mycar, true);
echo $obj_str;
?>
Once you run the above example, you should see and output with the objected converted to a string.
PHP Magic Methods (_toString())
The second technique we can use to convert an object to a string is the _toString() function. This function is defined as part of the PHP magic function. The _toString() function accepts no arguments and has a return value of string.
The following example shows how to use the function to return an object as a string.
class Car {
private $name;
private $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
public function __toString() {
return "Car Model $this->name. Price $this->price";
}
}
$mycar = new Car("Ford", "25000");
echo $mycar;
?>
The example above is closely identical to the previous one. However, we use the __toString method to convert the object to a string. The output of the above example is as shown:
If you remove the __toString() method and try to run the command above, you should see an error as:
PHP Serialize() Function
Let us now look at a more non-conventional way to convert an object to a string. The serialize() method converts a specified value to a byte-stream. This function is proper when you need to store a session. Using the serialize() function, we can pass the object and return a byte sequence.
An example usage can be shown in the code below.
class Car {
private $name = "Ford";
private $price = "25000$";
public function which_car() {
echo "Model: $this->name and Price: $this->price", "\n";
}
}
$mycar = new Car;
$mycar->which_car();
$serialized = serialize($mycar);
echo $serialized;
?>
Once we run the above code, the code should return a serialized string as shown:
O:3:"Car":2:{s:7:"*name";s:4:"Ford";s:8:"*price";s:6:"25000$";}
Closing
This tutorial shows you various ways to convert an object into a string using PHP. Keep in mind that there is more to the functions than discussed in this tutorial. Look in the documentation to find out more.