Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Author by Kay Sackey
- Many thanks to http://latrine.dgx.cz/property-setters-and-getters-final-solution
- PHP Property overloading
- class ATest extends Magic {
- // After the constructor fires this property is 'hidden', but you can access it directly through $_myAttribute
- protected $myAttribute = property;
- public function __construct(){
- parent::__construct(); // Remember to call the Magic constructor first if your class defines a constructor
- }
- // I use public functions for getters and setters because I only use this class to transition from class attributes towards getters/setters
- // You can use it anyway you like.
- // If you don't define a getter or a setter then he object attribute will behave just as you expect it.
- // e.g. $class->a = 5
- public function getMyAttribute(){
- // Do some complex logic to get the attribute
- return 0;
- }
- public function setMyAttribute(){
- // Do some complex logic to set the attribute
- }
- */
- //
- // This has only been updated to not throw a notice under E_STRICT, and to make it easier to integrate into existing classes
- // define new "like-keyword"
- // value is short and unusual string
- define('property', "\0\0");
- class Magic{
- // magic
- protected $_props;
- /**
- * Universal setter.
- *
- * If property 'abc' is declared,
- * there must be setAbc() setter,
- * otherwise property is read-only
- */
- final private function __set($name, $value)
- {
- if (isset($this->_props[$name]))
- if (method_exists($this, 'set'.$name))
- $this->{'set'.$name}($value);
- else
- throw new Exception("Property '$name' is read-only.");
- elseif($name[0] === '_')
- $this->$name = $value;
- else
- throw new Exception("Undefined property '$name'.");
- }
- /**
- * Universal getter.
- *
- * If property 'abc' is declared,
- * there must be getAbc() getter,
- * otherwise variable $_abc
- */
- final private function __get($name)
- {
- if (isset($this->_props[$name]))
- if (method_exists($this, 'get'.$name))
- return $this->{'get'.$name}();
- else
- return $this->{'_'.$name};
- else
- throw new Exception("Undefined property '$name'.");
- }
- final private function __isset($name)
- {
- return isset($this->_props[$name]);
- }
- final private function __unset($name)
- {
- unset($this->_props[$name]);
- }
- final private function _property($obj)
- {
- // use cache (analyze object only once)
- static $cache;
- $item = & $cache[ get_class($obj) ];
- // look for properties
- // 1) get_object_vars prevents ObjectIteration
- // 2) outside of objects returns only public members (speed-up)
- if ($item === NULL) {
- $item = array();
- foreach (get_object_vars($obj) as $name => $value){
- if ($value === property) {
- $item[$name] = true;
- unset($obj->$name);
- $obj->{'_'.$name} = null;
- }
- }
- return $item;
- }
- foreach ($item as $name => $foo){
- unset($obj->$name);
- $obj->{'_'.$name} = null;
- }
- return $item;
- }
- function __construct()
- {
- // property initialization
- $this->_props = self::_property($this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment