Advertisement
Guest User

ClassAbstract.php

a guest
Sep 30th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class ClassAbstract
  4. {
  5.     public function __call($name, $args)
  6.     {
  7.         $methodPrefix   = substr($name, 0, 3);
  8.         $methodProperty = strtolower($name[3]) . substr($name, 4);
  9.        
  10.         switch ($methodPrefix) {
  11.             case "set":
  12.                 if (count($args) == 1)
  13.                     $this->__set($methodProperty, $args[0]);
  14.                 else
  15.                     throw new Exception("magic method only supports one argument");
  16.                 break;
  17.             case "get":
  18.                 return $this->__get($methodProperty);
  19.                 break;
  20.             default:
  21.                 throw new Exception("magic methods only supported for get and set");
  22.         }
  23.     }
  24.     public function __set($name, $value)
  25.     {
  26.         if (property_exists($this, $name))
  27.             $this->$name = $value;
  28.         else
  29.             throw new Exception("$name doesn't exist in this class");
  30.     }
  31.     public function __get($name)
  32.     {
  33.         if (property_exists($this, $name))
  34.             return $this->$name;
  35.         else
  36.             throw new Exception("$name doesn't exist in this class");
  37.     }
  38.    
  39.    
  40. }
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement