From: Marcus Boerger Date: Mon, 30 Aug 2004 18:49:49 +0000 (+0000) Subject: MFH: Add some information about array overloading X-Git-Tag: php-5.0.2RC1~59 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=25ab1857613cb5300395989713b97dda14154446;p=php MFH: Add some information about array overloading --- diff --git a/Zend/ZEND_CHANGES b/Zend/ZEND_CHANGES index 81f14d2793..5208ce1623 100644 --- a/Zend/ZEND_CHANGES +++ b/Zend/ZEND_CHANGES @@ -890,6 +890,47 @@ Changes in the Zend Engine 2.0 the use of all abstract methods declared in the interfaces Iterator and IteratorAggregate respectively. + * Array overloading + + Objects can be used with Array notation when they implement the + interface ArrayAccess. You cannot use such objects in standard + array functions, however you have full control over the array + notation. This allows lazy initialization or read only array. + + Note that setting [] results in a call to offsetSet() with + index being NULL. That means that as with standard arrays you + cannot store NULL keys. + + Example: + + a); + } + function offsetGet($index) { + return $this->a[$index]; + } + function offsetSet($index, $newval) { + return $this->a[$index] = $newval; + } + function offsetUnset($index) { + unset($this->a[$index]); + } + } + + $obj = new ArrayClass; + + $obj[0] = 'bla'; // calls offsetSet(0,'bla') + $obj[] = 42; // calls offsetSet(NULL, 42) + $x = $obj[0]; // calls offsetGet(0) + $b = isset($obj[0]); // calls offsetExists(0) + unset($obj[0]); // calls offsetUnset(0) + ?> + + * __METHOD__ The pseudo constant __METHOD__ shows the current class and method