Tuesday, January 22, 2013

Objects

Objects

Object Initialization

To initialize an object, you use the new statement to instantiate the object to a variable.
<?phpclass foo{
    function 
do_foo()
    {
        echo 
"Doing foo.";
    }
}
$bar = new foo;$bar->do_foo();?>

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. 
Array converts to an object with properties named by array keys
 and with corresponding values. For any other value, a member
 variable named scalar will contain the value.
<?php
$obj 
= (object) 'ciao';
echo 
$obj->scalar;  // outputs 'ciao'?>

No comments:

Post a Comment