Advertisement
Guest User

Eryr

a guest
Sep 25th, 2010
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1.     /**
  2.      * Magic __call method
  3.      *
  4.      * Attempt to set/get a property. Only supports all-lowercase properties.
  5.      * This differs from __set() and __get(), as we want to be able to have custom get()
  6.      * and set() methods (e.g. setPassword() encrypts the password before setting it).
  7.      *
  8.      * @access  public
  9.      * @param   string  $method
  10.      * @param   mixed   $args
  11.      * @return  void|mixed
  12.      */
  13.     public function __call($method, $args)
  14.     {
  15.         //Find words by camelCase (e.g. setUsername, getUserGroup)
  16.         $method_words = preg_split('/(?=[A-Z])/', $method);
  17.         $method_name = $method_words[0];
  18.        
  19.         //Remove the method name from method_words
  20.         unset($method_words[0]);
  21.        
  22.         //The remaining method_words make up the property name
  23.         //UserGroup becomes user_group
  24.         $property = strtolower(implode('_', $method_words));
  25.  
  26.         //Property doesn't exist
  27.         if (! property_exists($this, $property))
  28.         {
  29.             show_error("<strong>Fatal Error:</strong> Tried to call {$method}() on " . __CLASS__ . ". Property '{$property}' doesn't exist.");
  30.         }
  31.  
  32.         //Set() methods
  33.         if ($method_name == 'set')
  34.         {
  35.             //More than one argument was given
  36.             if (count($args) > 1)
  37.             {
  38.                 show_error("<strong>Fatal Error:</strong> Tried to set " . __CLASS__ . "->{$property}. 1 argument expected; " . count($args) . " given.");
  39.             }
  40.  
  41.             $this->$property = $args[0];
  42.         }
  43.  
  44.         //Get() methods
  45.         if ($method_name == 'get')
  46.         {
  47.             return $this->$property;
  48.         }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement