So I was working on a little framework for a mini application I'm going to build soon and I decided to use object overloading because it's written in PHP5. If you haven't used the overloading functions in PHP5, they are: __get, __set, __isset, and __unset. You can read more about them on PHP's overloading page.
As I've said, this is a PHP5 framework and so I decided to make use of the ArrayAccess interface. However, I haven't actually used this interface in a while and forgot the actual functions that it requires: offsetGet, offsetSet, offsetExists, and offsetUnset. Intuitively, I thought that it must use the overload functions (__get, et al.) and so programmed everything assuming that. Unfortunately when I actually got around to test everything, there were errors. At first I decided to go in and make the minor name switches, but it being a framework, I decided if anyone but me were using it, I could have a better solution. Vague enough? Here's the nifty little BetterAccess class I came up with:
abstract class Overload implements ArrayAccess { abstract public function __get($key); abstract public function __set($key, $val); abstract public function __isset($key); abstract public function __unset($key); final public function offsetGet($key) { return $this->__get($key); } final public function offsetSet($key, $val) { return $this->__set($key, $val); } final public function offsetExists($key) { return $this->__isset($key); } final public function offsetUnset($key) { return $this->__unset($key); } }This class allows for PHP objects to act like Javascript ones insofar as class properties can be accessed via array syntax or object syntax.
class Foo extends Overload { private $vars = array(); final public function __get($key) { $ret = NULL; if(isset($this->vars[$key])) $ret = $this->vars[$key]; return $ret; } final public function __set($key, $val) { $this->vars[$key] = $val; } final public function __isset($key) { return isset($this->vars[$key]); } final public function __unset($key) { unset($this->vars[$key]); } }
$foo = new Foo; $foo['bar'] = 'hi'; $foo->baz = 'hello'; echo $foo->bar; echo $foo['baz']; // Output: hihello
This might not seem immediately useful; however, the point of it is taste. Depending on your coding preferences, Overload allows you to access a class either through overloading or using array access.