Enforcing property access using magic accessor methods : __get « Class « PHP






Enforcing property access using magic accessor methods

 
<?
class Person {

    protected $__data = array('person', 'email');

    public function __get($property) {
        if (isset($this->__data[$property])) {
            return $this->__data[$property];
        } else {
            return false;
        }
    }

    public function __set($property, $value) {
        if (isset($this->__data[$property])) {
            return $this->__data[$property] = $value;
        } else {
            return false;
        }
    }
}
?>
  
  








Related examples in the same category

1.__get( ) specifies what to do if an unknown property is read from within your script
2.Intercepting Property Access with __get() and __set() (PHP 5 Only)