Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.80 KB | None | 0 0
  1. abstract class GetterSetter{
  2.     protected $variables = array();
  3.    
  4.     public function __get($name){
  5.         if (isset($this->variables[$name]){
  6.             return $this->variable[$name]['value'];
  7.         }
  8.         throw new Exception($name . " not a member");
  9.     }
  10.    
  11.     public function __set($name,$value){
  12.         if (isset($this->variable[$name]){
  13.             if (isset($this->variables[$name]['protected'] && $this->variables[$name]['protected']) throw new Excpetion('not allowed to edit');
  14.             $this->variables[$name]['value']=$value;
  15.         }
  16.         throw new Exception($name . " not a member");      
  17.     }
  18. }
  19.  
  20. class Example extends GetterSetter{
  21.     protected $variables = array(
  22.         'foo' => array('protected'=>false,'value'=>'bar'),
  23.         'bar' => array('protected'=>true , 'value'=>'foo')
  24.     );
  25. }
  26.  
  27. $a = new Example;
  28. $a->foo='a';
  29. $a->bar ='b'; //throws an error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement