Object Overloading : Objects « Class « PHP






Object Overloading

 
<?php
class Data {
    private $data = array();

    public function __set($name, $value) {
        $this->data[$name] = $value;
    }

    public function __get($name) {
        if (isset($this->data[$name])) { return $this->data[$name]; }
    }

    public function __isset($name) {
        return isset($this->data[$name]);
    }

    public function __unset($name) {
        unset($this->data[$name]);
    }
}

$data = new Data();
$data->name = 'F';

echo "<p>The data value of 'name' is {$data->name}</p>";

unset($data->name);
echo '<p>The value is ', isset($data->name) ? '' : 'not ', 'set.</p>';
?>
  
  








Related examples in the same category

1.Comparing Objects with == and ===
2.Create a new class and create an instance then use its property and method
3.Object Initialization
4.Object Properties
5.Object Type Information
6.Objects Within Objects
7.Creating a new object and assigning it to a variable