Overview
KB
Technical FAQ
PHP Manual
CSS2 Manual
HTML Manual
JS Guide
JS Reference
PhpDock Manual
Nu-Coder Manual
PhpExpress Manual
PHP Joomla
Development
Learn PHP
 
<ArraysEnumerations>
^TypesLast updated: Mon, 26 Oct 2026

Objects

Object Initialization

To create a new object, use the new statement to instantiate a class:

Example #1 Object Construction

<?php
class foo
{
    function do_foo()
    {
        echo "Doing foo.";
    }
}

$bar = new foo;
$bar->do_foo();
?>

For a full discussion, see the Classes and Objects chapter.

Converting to object

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 with their corresponding values. Prior to PHP 7.2.0, numeric keys would have been inaccessible unless iterated.

Example #2 Casting to an Object

<?php
$obj = (object) array('1' => 'foo');
var_dump(isset($obj->{'1'})); // outputs 'bool(true)'

// Deprecated as of PHP 8.1
var_dump(key($obj)); // outputs 'string(1) "1"'
?>

For any other value, a member variable named scalar will contain the value.

Example #3 (object) cast

<?php
$obj = (object) 'ciao';
echo $obj->scalar;  // outputs 'ciao'
?>

<ArraysEnumerations>
^TypesLast updated: Mon, 26 Oct 2026