PHP | Type Casting and Conversion of an Object to an Object of other class - GeeksforGeeks

09 Jan, 2019
Given a PHP class object and the task is to convert or cast this object into object of another class.
Approach 1: Objects which are instances of a standard pre-defined class can be converted into object of another standard class.
Example:
<?php// PHP program to show // standard type casting  $a = 1;var_dump($a);  // int to float$a = (float) $a;var_dump($a);  // float to double$a = (double) $a;var_dump($a);  // double to real$a = (real) $a;var_dump($a);  // real to int$a = (int) $a;var_dump($a);  // int to integer$a = (integer) $a;var_dump($a);  // integer to bool$a = (bool) $a;var_dump($a);  // bool to boolean$a = (boolean) $a;var_dump($a);  // boolean to string$a = (string) $a;var_dump($a);  // string to array$a = (array) $a;var_dump($a);  // array to object$a = (object) $a;var_dump($a);  // object to unset/NULL$a = (unset) $a;var_dump($a);  ?>
int(1)
float(1)
float(1)
float(1)
int(1)
int(1)
bool(true)
bool(true)
string(1) "1"
array(1) {
  [0]=>
  string(1) "1"
}
object(stdClass)#1 (1) {
  [0]=>
  string(1) "1"
}
NULL

Approach 2: Create a constructor for final class and add a foreach loop for assignment of all properties of initial class to instance of final class.
Example:
<?php// PHP program to convert an class object// to object of another class  // Original classclass Geeks1 {          var $a = 'geeksforgeeks';          function print_geeksforgeeks() {        print('geeksforgeeks');    }}  // Final classclass Geeks2 {      // Constructor function of class Geeks2    public function __construct($object) {          // Initializing class properties        foreach($object as $property => $value) {            $this->$property = $value;        }    }}  // Initializing an object of class Geeks1$object1 = new Geeks1();  // Printing original object of class Geeks1print_r($object1);  // Initializing an object of class Geeks2// using an object of class Geeks1$object1 = new Geeks2($object1);  // Printing object of class Geeks2print_r($object1);  ?>
Geeks1 Object
(
    [a] => geeksforgeeks
)
Geeks2 Object
(
    [a] => geeksforgeeks
)

Approach 3: Write a function to convert object of the initial class into serialized data using serialize() method. Unserialize this serialized data into instance of the final class using unserialize() method.Note: Member functions cannot be transferred using this approach. This approach can only be used if initial class contains only variables as members.
Example:
<?php// PHP program to convert an class object // to object of another class  // Original classclass Geeks1 {          var $a = 'geeksforgeeks';          function print_geeksforgeeks() {        print('geeksforgeeks');    }}  // Final classclass Geeks2 {          /* Empty abstract class */}  // Function to convert class of given objectfunction convertObjectClass($object, $final_class) {    return unserialize(sprintf(        'O:%d:"%s"%s',        strlen($final_class),        $final_class,        strstr(strstr(serialize($object), '"'), ':')    ));}  // Initializing an object of class Geeks2$object1 = new Geeks1();  // Printing original object of class Geeks1print_r($object1);  // Converting an object of class Geeks1// into an object of class Geeks2$object1 = convertObjectClass($object1, 'Geeks2');  // Printing object of class Geeks2print_r($object1);  ?>
Geeks1 Object
(
    [a] => geeksforgeeks
)
Geeks2 Object
(
    [a] => geeksforgeeks
)

Note: In general, PHP doesn’t allow type casting of user defined classes, while conversion/casting can be achieved indirectly by approaches presented above.
PHP-basics
PHP-OOP
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to execute PHP code using command line ?
How to pop an alert message box using PHP ?
PHP in_array() Function
How to convert array to string in PHP ?
How to Insert Form Data into Database using PHP ?
How to execute PHP code using command line ?
How to pop an alert message box using PHP ?
How to convert array to string in PHP ?
How to delete an array element based on key in PHP?