Change object to string php

For any Objected Oriented Programming Language, the concepts of Objects and Strings are fundamental building blocks. PHP relies on objects and strings to provide flexibility and features. This article will discuss how you can convert a PHP object to a string using various methods.

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:

print_r[mixed $value, bool $return];

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:

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.

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:

Car Model Ford. Price 25000

If you remove the __toString[] method and try to run the command above, you should see an error as:

PHP Fatal error: Uncaught Error: Object of class Car could not be converted to string.

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.

Once we run the above code, the code should return a serialized string as shown:

Model: Ford and Price: 25000$
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.

About the author

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

How do you turn an object into a string?

Convert Object to String in java using toString[] method of Object class or String. valueOf[object] method. Since there are mainly two types of class in java, i.e. user-defined class and predefined class such as StringBuilder or StringBuffer of whose objects can be converted into the string.

How convert class to string PHP?

Convert Object to String in PHP.
Use the __toString[] Magic Method to Convert an Object to a String in PHP..
Use the serialize[] Function to Convert an Object to a String in PHP..
Use the print_r[] Function to Convert an Object to a Strig in PHP..

How to echo object value in PHP?

PHP is_object[] Function echo "
";
var_dump[get_cars[$obj]];

How to print object name in PHP?

You have several way to print a a class name in php: get_class: Returns the name of the class of an object. You will have a warning if the function is called on a non object. ClassName::class: Since PHP 5.5, we can access to the fully qualified name of a class.

Chủ Đề