An object is a data type that stores other data, information and functions. An object is created by declaring a class object which is actually a structure containing properties and methods. To create an object, a class is to be declared:

<?php
    class newObject {
        function func1() {
            echo "This is function1";
        }
        function func2() {
            echo "This is function2";
        }
    }
    
    $var = new newObject;
    $var -> func1();
?>

If a value of a data type (except if it's an object) is converted into an object, a new instance of the stdClass built-in class is created.

A null value will create an empty new instance.

An array can convert into an object with properties named by keys and corresponding values, as shown in the example below:

<?php
    $obj = (object) array("1" => "one");
    var_dump(isset($obj -> {"1"})); // outputs "bool"(false)"
    var_dump(key($obj)); // outputs "int(1)"
?>

 

To learn more about PHP objects and classes follow this link.

 

›› go to examples ››