Posts Tagged ‘getter’
PHP Magic Methods - enforce defining of class properties
Sometimes it’s desirable to have a control over which properties can be assigned to an object. Typo mistakes, duplicating of property names, among others, are the commons pitfalls of PHP’s ability to set/get properties without defining them in a class.
The MagicMethods class enforces that class properties are defined in a class before we can set or get their values. Any class that extends this class will benefit from this behavior (unless magic method get overridden in the exteding class). If a getter or setter method is needed to manipulate a property, MagicMethod class will use these instead of directly accessing the value.
This functionality is especially handy for value objects or config classes.
Example:
class ConfigMagic extends MagicMethods_Abstract { // note $name is protected, not private, so that MagicMethods_Abstract can access it protected $name; public function getName() { // provides more complex way of getting a variable return $this->name; } public function setName($value) { // provides more complex way of setting a variable $this->name = $value; } } $config = new ConfigMagic(); $config->name = 'magic'; var_dump($config->name); // string 'magic' (length=5) var_dump(isset($config->name)); // true unset($config->name); var_dump(isset($config->name)); // false // Exception: Cannot set property 'nonExisting'. Not defined in class ConfigMagic ... $config->nonExisting = 'magic';
Download the files:
MagicMethods_Abstract.php
MagicMethodsException.php
/** * Magic Methods * Provides magic methods for other classes, enforces property names. * Undefined property name throws an exception * * @package DAO * @uses MagicMethodsException * @example MyOwnClass extends MagicMethods_Abstract * @license Free to use and modify (including commercial use) * @see http://php.net/manual/en/language.oop5.magic.php * @author Juraj Seffer * @copyright 2009, Juraj Seffer * @version $Id$ * @access public */ abstract class MagicMethods_Abstract { /** * Magic getter * * @param string $property * @access protected * @return mixed */ protected function __get($property) { $getterMethod = "get" . strtoupper($property); if (method_exists($this, $getterMethod)) { return $this->$getterMethod(); } else if (!property_exists($this, $property)) { throw new MagicMethodsException( "Cannot get property '$property'. Not defined in class " . get_class($this) ); } else { return $this->$property; } } /** * Magic setter * * @param string $property * @param mixed $value * @access protected * @return void */ protected function __set($property, $value) { $setterMethod = "set" . strtoupper($property); if (method_exists($this, $setterMethod)) { return $this->$setterMethod($value); } else if (!property_exists($this, $property)) { throw new MagicMethodsException( "Cannot set property '$property'. Not defined in class " . get_class($this) ); } else { $this->$property = $value; } } /** * Magic isset * * @param string $property * @access protected * @return mixed */ protected function __isset($property) { if (!property_exists($this, $property)) { throw new MagicMethodsException( "Property '$property' not defined in class " . get_class($this) ); } else { return isset($this->$property); } } /** * Magic unset * * @param string $property * @access protected * @return void */ protected function __unset($property) { if (!property_exists($this, $property)) { throw new MagicMethodsException( "Property '$property' not defined in class " . get_class($this) ); } else { $this->$property = null; } } }
* @copyright 2009, Juraj Seffer * @version $Id$ * @access public */ class MagicMethodsException extends Exception { /** * Constructor * * @param string $exception * @access public * @return void */ public function __construct($exception) { throw new Exception($exception); } }