MOCKSTACKS
EN
Questions And Answers

More Tutorials




Cast generic type parameter to a specific type in PHP


Objects which are instances of a standard pre-defined class can be converted into object of another standard class.

<?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);

?>

Output

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

Create a constructor for final class and add a foreach loop for assignment of all properties of initial class to instance of final class.

<?php
// PHP program to convert an class object
// to object of another class

// Original class
class Geeks1 {
	
	var $a = 'geeksforgeeks';
	
	function print_geeksforgeeks() {
		print('geeksforgeeks');
	}
}

// Final class
class 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 Geeks1
print_r($object1);

// Initializing an object of class Geeks2
// using an object of class Geeks1
$object1 = new Geeks2($object1);

// Printing object of class Geeks2
print_r($object1);

?>

Output

Geeks1 Object
(
[a] => geeksforgeeks
)
Geeks2 Object
(
[a] => geeksforgeeks
)


Conclusion

In this page (written and validated by ) you learned about . What's Next? If you are interested in completing PHP tutorial, we encourage you simply to start here: PHP Tutorial.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.