object: Converting to object

Converting to object

<?php
$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)' as of PHP 7.2.0; 'bool(false)' previously
var_dump(key($obj)); // outputs 'string(1) "1"' as of PHP 7.2.0; 'int(1)' previously
?>
  • If an object is converted to an object, it is not modified.
  • If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created.
  • If the value was NULL, the new instance will be empty.
  • An array converts to an object with properties named by keys and corresponding values.
  • For any other value, a member variable named scalar will contain the value.

Related concepts

object: Converting to object — Structure map

Clickable & Draggable!

object: Converting to object — Related pages: