Array access / iteration / type boxing / genericsScope of ChangeThe lang.types.ArrayList class and the relevant classes from util.collections will be changed to support array access (read/write) and foreach() iteration. The classes from the collections API will furthermore support type boxing and provide generic variants of themselves. The text.String class will be deprecated and replaced by lang.types.String and lang.types.Character. Rationale
Easier / intuitive interface to "array" types. Functionality
Class overview
* lang.types.ArrayList is an immutable, zero-indexed list of values of any type and most closely resembles an array in Java. * util.collections.Vector is a resizable array of objects. * util.collections.HashTable is a map where both keys and values are objects.
Array access
The ArrayAccess interface requires implementing the following methods:
offsetGet(), offsetSet(), offsetExists() and offsetUnset(). These
overload getting ($value= $object[$key]), setting ($object[$key]= $value)),
testing (isset($object[$key])) and removing (unset($object[$key])). with ($a= new ArrayList(1, 2, 3)); {
Iteration
The IteratorAggregate interface requires implementing a method called
getIterator() which must return an Iterator instance. This makes a class
usable inside the foreach statement. // ArrayList foreach-iteration, prints "1 2 3 "
Generics
To be able to create generics with the "new" keyword, we would need to
extend PHP's syntax or change constructor signatures. Both of those have
obvious downsides. lang.Generic create(mixed arg) throws IllegalArgumentException To create a generic object, the argument passed to create() must be a string
of the form ClassType "<" ComponentType [ ", " ComponentType ] ">". Class
names referenced by ClassType and ComponentType may be qualified or not and
need to refer to loaded XP classes or interfaces. with ($hash= create('HashTable<String, String>')); {
Supporting generics in classes
To add generics support to a class, a public member named "__generic" must
be introduced. In all methods, the generic types must be verified manually. class SortedList extends Object {
This poses a higher burden when programming (compared to one being able to
express this syntactically) but does not introduce a great performance
penalty like http://experiments.xp-framework.net/?people,friebe,generics
does (transforms syntactically defined generic declarations to the notation
seen above when a class is loaded). Additional note: create() overloaded
The create() functionality provides an overloaded version of itself:
When passed a lang.Generic instance, the instance is simply returned. echo new Date()->toString(); // Parse error The following is the workaround: echo create(new Date())->toString();
Additional note: ArrayList vs. Vector
The distinction between those two is that ArrayList is a thin, immutable
wrapper type without utility functions such as indexOf() or contains(),
whereas the Vector class provides a full "list" API. Additional note: ArrayList constructor
lang.types.ArrayList's constructor will be changed to accept varargs
instead of an array. The classes that use lang.types.ArrayList will
be adapted to this change. // Constructing an arraylist from an array
Security considerations
n/a Speed impact
Slightly slower (more methods, extra implementation checks) Dependencies
- A new interface util.collections.IList will be introduced. An
implementation will exists in form of the new class
util.collections.Vector. Related documents
- http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html
Zend-Engine internal interface ArrayAccess Comments
- friebe, Mon Jan 8 19:09:55 2007
The ArrayAccess and IteratorAggregate interfaces are part of the Zend
Engine, not of SPL! | Table of contents |