ArrayAccess vs ArrayObject
Join the DZone community and get the full member experience.
Join For Free
i help people qualify for
zend certification
and in the last few months i've had questions about both
arrayaccess and
arrayobject
. this post is an attempt to illuminate both.
in very simple terms, arrayaccess is an interface, which you can implement in your own objects; arrayobject, on the other hand, is a class, which you can either use or extend.
arrayaccess
arrayaccess
is an interface built in to php which lets you dictate how php behaves
when an object has array syntax (square brackets) attached to it.
interfaces are collections of function prototypes that we can use as
templates for our own code. if you read the manual page for
arrayaccess, it shows four functions:
arrayaccess { /* methods */ abstract public boolean offsetexists ( mixed $offset ) abstract public mixed offsetget ( mixed $offset ) abstract public void offsetset ( mixed $offset , mixed $value ) abstract public void offsetunset ( mixed $offset ) }
to implement this interface, we just declare all these methods in our class. here is an example class:
class prettybasket implements arrayaccess { protected $contents = array(); public function offsetexists($index) { return isset($this->contents[$index]); } public function offsetget($index) { if($this->offsetexists($index)) { return $this->contents[$index]; } return false; } public function offsetset($index, $value) { if($index) { $this->contents[$index] = $value; } else { $this->contents[] = $value; } return true; } public function offsetunset($index) { unset($this->contents[$index]); return true; } public function getcontents() { return $this->contents; } }
in addition to accessing the properties and methods of a basket object, we can also use array notation with it, so we can write code along these lines.
$basket = new prettybasket(); $basket->madeof = 'wicker'; $basket->capacity = '14'; $basket[] = 'pie'; $basket[] = 'apples'; $basket[] = 'map'; // $contents = $basket->getcontents(); //echo $contents[0]; echo $basket[0];
the $basket variable is a basket object, but we're using $basket[] notation with the object, so how does that work? in fact when we do this, php will check if we implement the arrayaccess interface, and if we do then it calls our methods. this is very neat and can make for an easy way to give quick access to an array stored in one of the object properties, as we've done here. you can see that there's some code commented out and replaced by the one line following it - it's much shorter which means this is attractive for using to output data into templates. another good example is to create behaviour such as simplexmlelement has, which uses this array notation to present the attributes of a tag.
arrayobject
arrayobject is an object, you can implement and extend it as you usually would. it implements a bunch of useful interfaces and wraps them up into an object. for creating an object to look a lot like an array, take a look at arrayobject! it implements arrayaccess as we saw above, but also countable, traversable, and others.
the upshot is a class which, out of the box, can give us the behaviour shown above, plus foreach behaviour which foreaches the array elements rather than the object properties, support for serialising the data, and more. this is very useful when we work with this kind of pattern (typically data storage patterns but i'm open to hearing stories about how you use it in your own code).
you can extend arrayaccess and create your own class, adding properties and methods in the usual way, and using the object exactly as you usually would ... except that it has all this array functionality built in as well! it's a powerful class but do bear in mind that although we can foreach it and access its individual elements, it won't look like an array to any of php's array functions. so for many applications, you do still want an array (or an object which can return you the array it's storing things in).
hopefully that gives a clearer idea of what these array* things are and when they'll be useful. are you implementing these yourself? i'd love some more practical case studies if you want to share.
Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
An Overview of Kubernetes Security Projects at KubeCon Europe 2023
-
Is Podman a Drop-in Replacement for Docker?
-
4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
-
Web Development Checklist
Comments