-
Notifications
You must be signed in to change notification settings - Fork 0
AeObject
AeObject is a basic class in AnEngine. It has some common functionality implemented for all subclasses to inherit. The basic functionalities provided by this class are:
- virtual property getters and setters;
- event handling methods;
- misc. class functions, like
getClass(),propertyExists(),methodExists().
AnEngine makes it easy for developers to create properties with their respective getters and setters. By default, AeObject creates a property getter and setter for any public and protected properties. It even recognizes a single leading underscore in protected property names. If the property is declared protected, any direct access attempts will be rerouted to use custom getters and setters (if provided) or default ones.
Both public and protected properties are supported by providing getters and setters, but there is a difference in how explicit property access is handled for these two types. Consider the following code:
class Test extends AeObject { public $a = 'a'; public $_b = 'b'; protected $c = 'c'; protected $_d = 'd';// we will define custom getters for the sake of example public function getA() { return 'getA.' . $this->a; }public function getB() { return 'getB.' . $this->_b; }public function getC() { return 'getC.' . $this->c; }public function getD() { return 'getD.' . $this->_d; } }$test = new Test;echo $test->a; // prints "a" echo $test->b; // prints "getB.b" echo $test->_b; // prinst "b"echo $test->c; // prints "getC.c" echo $test->d; // prints "getD.d" echo $test->_d; // prints "getD.d"
As you can see, custom getters are not called for public properties, that were accessed directly. The same is true for property setters. This is why declaring properties protected is a preferred method, since you can always add a custom property getter at a later stage without any need to modify existing code, that uses this property.
You can make a protected property unreachable by getters and setters by prepending its name with several underscores:
class Test extends AeObject { protected $_visible = 'foo'; /** * we use three underscores, as two underscores * might be associated with some kind of magic * functionality in PHP */ protected $___hidden = 'bar'; }$test = new Test;echo $test->visible; // prints "foo" echo $test->get('hidden', 'null'); // prints "null" echo $test->get('___hidden', 'null'); // prints "null"
There is one more feature that AeObject provides: any getter method created a read-only property. Adding a setter to an existing getter makes property writable. You can also create a write-only property, if you wish. This feature can be used with resource-associated classes, like files or images.
class Test extends AeObject { protected $_path;public function __construct($file) { $this->_path = realpath($file); }public function getChmod() { return substr(sprintf('%o', fileperms($this->path)), -4); }public function setChmod($value) { chmod($this->path, $value); } }/* * NOTE: this code does not have proper value * type checks and is only provided as a * demonstration of AeObject behavior */$test = new Test('./test.txt');echo $test->chmod; // prints "0664" or something similar $test->chmod = 0640;echo $test->chmod; // prints "0640"
Because virtual methods and properties are not actually implemented, property_exists() and method_exists() functions are not accurate. That is why, AeObject provides its own propertyExists() and methodExists() methods:
class Test extends AeObject { protected $_foo;public function getBar() { return 'bar'; }public function setBaz($value) { // some code here return $this; } }$test = new Test;// propertyExists() examples: echo $test->propertyExists('foo'); // true echo $test->propertyExists('bar'); // true echo $test->propertyExists('baz'); // falseecho $test->propertyExists('foo', 'set'); // true echo $test->propertyExists('bar', 'set'); // false echo $test->propertyExists('baz', 'set'); // true// methodExists() examples: echo $test->methodExists('getFoo'); // true echo $test->methodExists('getBar'); // true echo $test->methodExists('getBaz'); // falseecho $test->methodExists('setFoo'); // true echo $test->methodExists('setBar'); // false echo $test->methodExists('setBaz'); // true
The optional second argument for propertyExists() specifies which property mode is to be checked. Currently only get and set methods are supported.