Definition of PHP Object to String
PHP is a programming language which deals with objects and strings quite frequently, thus, to convert a PHP object into string there is a need of conversion where the objects get converted into Strings easily using _toString() function and serialize function. _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 assumed.
Syntax
There is no exact syntax which is extensively used to convert PHP object into String but there are other ways to achieve it for those conversions, syntax exist and are used by embedding them within the code which is represented as follows :
$var = some_name
$var = obj_1;
{
Call function with $var;
Use _toString() method;
}
Where,
var is some random variable taken into consideration and will hold the value for the PHP object. Once the function is being manipulated accordingly the function will get called which will have _toString() method in it and it will help in making the object convert into byte stream of string.
This same thing can be achieved using serialize() method as well but it will have some more variation into it.
How to Convert Object to String in PHP?
- There are numerous ways to convert object to string because till PHP 4.0 versions it was easy to get the variable with object and then to convert that object into string. After the object conversion into string it is very much necessary to get the detailed info about the object being considered for the manipulation.
- There is no single or say process to convert an object to string in PHP after version 5 and above but still there are certain ways to indirectly convert that object to string.
- There are certain approaches to achieve it and the approaches involve conversion like using the _toString() function which is considered as a magic function and with serialize method another method to expose and get the byte stream of string to fetch necessary information about the object.
- _toString() function is considered one of the magic functions after lot of time because it helps in converting the entire object into string.
- Serialize() method involves object related to array and decoding of array object like this method will check for the class which has a function with magic name for consideration.
- The main motive behind the usage of Serialize() method is to represent the object that needs to be converted into string for fetching and retrieving the values from that method. Elements of the object present may correspond to the properties of that object but that is again not mandatory to be used for.
- On the other hand there is somewhere the requirement to use unserialized() method which have some differences when compared with serialize() method that represents the object which says that unserialized() method is mostly used to check the presence of magic number that will have values to be passed from the function that will pass the restored array through that object into consideration.
- Both serialize and unserialize method gets used with the object and then they are compatible only with the PHP version 5.0 and above with _toString() method in conjunction.
- Then there is another method which is considered for the same as json encoding which will help in encoding the information related to that object from within the function and it can simultaneously use json decoding for making the entire string to decode the object.
- There exists certain errors and exceptions which are also part of the PHP object to string conversion and is readily used to make certain exposed and profound manipulation within the object and consequent series of array.
- Overall one of the magic method _toString() method is used to convert the object to byte stream of String easily.
Examples of PHP Object to String
Following are the examples are given below:
Example #1
This program demonstrates the serialize() method which returns the byte stream of string representing the value as shown in the output.
Code:
<?php
class Nw_Ob {
public $nme = 'Henry_Joe';
public function __toString() {
return "person nme is: {$this->nme}\n";
}
}
$OBJ_1 = new Nw_Ob;
echo $OBJ_1;
echo serialize($OBJ_1);
?>
Output:
Example #2
This program demonstrates the conversion of object to string using values to be the part of an entire array of the object and it will return the entire set of strings as shown in the output. This functionality is more compatible with PHP version 5 and above.
Code:
<?php
$vl_1 = (object) array('almond' => 2, 'nuts' => 3, 'peanut' => 4);
$re_2 = new ReflectionObject($vl_1);
echo $re_2->getName() .' {' . implode(', ', array_map(
function($p_0) use ($vl_1)
{
$p_0->setAccessible(true);
return $p_0->getName() .': '. $p_0->getValue($vl_1);
}, $re_2->getProperties())) .'}';
Output:
Example #3
This program demonstrates the exception and error that gets represented once it is being showcased and the object is represented with string as shown in the output.
Code:
<?php
class M_Clss
{
public $nm;
public function __construct($nm)
{
$this->_name_1 = $nm;
}
public function __toString()
{
try
{
return (string) $this->_name_1;
}
catch (Exception $exception)
{
return '';
}
}
}
$cls_Obj = new M_Clss('Jhon_ops');
echo $cls_Obj;
?>
Output:
Example #4
This program demonstrates the input of an array with fruits which will be used to expose by using the JSON encode function which will be used to get the object to encode as shown in the output.
Code:
<?php
$arr_01 = array('apple' => 2, 'kiwi' => 4, 'mango' => 6, 'banana' => 8, 'orange' => 9);
echo json_encode($arr_01);
?>
Output:
Conclusion
Conversion of PHP object into String plays a crucial role as it helps the programmers get the insight knowledge and lot of details about the object which will be used for conversion into byte stream of string that will be used for making the entire structuring with the object result or returned string to be manipulated further as per requirement.
Recommended Articles
This is a guide to PHP Object to String. Here we also discuss the introduction, syntax, and examples to convert an object to a string in PHP along with code implementation. You may also have a look at the following articles to learn more –
5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses