News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

DevShed:
Implementing the ArrayAccess Interface - PHP
June 17, 2011 @ 08:58:04

In the first part of a new series over on DevShed.com, they introduce the concept of "segregated interfaces" and show how to use them to work with collections of arrays (using interfaces that are a part of the SPL).

To start illustrating why segregated interfaces are really useful, in the lines to come I'm going to build an example that will recreate the scenario described in the introduction. Basically, what I want to achieve here is to construct a custom countable array collection.

He shows the basic class structure needed to emulate a countable array in an object by implementing the "Countable" interface. He adds in the "Iterator" interface to allow you to work with the dataset like an array - progressing through it, rewinding to the beginning and checking to see if a value exists. Finally, they add the "ArrayAccess" interface to the class that boosts it with even more features like the ability to grab things by specific keys (numeric or string). The finish the article off with an example of an ArrayCollection object and how it can be looped through using a foreach.

0 comments voice your opinion now!
tutorial arrayaccess interface countable iterator spl


blog comments powered by Disqus

Similar Posts

Cody Taylor's Blog: Checking Bits With PHP

SitePoint PHP Blog: How to Build an OctoberCMS Theme

Gennady Feldman's Blog: Installing Oracle Instant Client 11g and PECL OCI8 (Part 2)

Brandon Savage's Blog: A Closer Look At ArrayObject

DevShed: An Introduction to Simulating the Model-View-Controller Schema in PHP


Community Events

Don't see your event here?
Let us know!


example series opinion performance interview extension framework community voicesoftheelephpant release conference install php7 introduction library podcast laravel api symfony2 version

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework