This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

PHP Casting

Cast to Object


object(stdClass)#1 (1) {
  ["scalar"]=>
  int(5)
}
object(stdClass)#2 (1) {
  ["scalar"]=>
  float(5.34)
}
object(stdClass)#3 (1) {
  ["scalar"]=>
  string(5) "hello"
}
object(stdClass)#4 (1) {
  ["scalar"]=>
  bool(true)
}
object(stdClass)#5 (0) {
}

When converting into objects, most data types converts into a object with one property, named "scalar", with the corresponding value.

NULL values converts to an empty object.

<?php
// data types to be cast to objects
$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

// casting data types to objects
$a = (object) $a;
$b = (object) $b;
$c = (object) $c;
$d = (object) $d;
$e = (object) $e;

// To verify the type of any object in PHP, use the var_dump() function:
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>