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

SitePoint PHP Blog:
Fun with Array Interfaces
Dec 09, 2013 @ 10:53:06

On the SitePoint PHP Blog a tutorial has been posted recently about having some fun with array interfaces via some of the functionality provided through the SPL (Standard PHP Library).

As a programmer who works with different languages every day, I find a lot of joy in learning how things are done differently in other languages and seeing if I can do the same in PHP. One thing I liked in particular in Python was how one can emulate features of native data types in custom classes. [...] I thought it would be nice if you could do the same in PHP on an instance of your custom classes and not only arrays. PHP lets us do this with array interfaces.

He illustrates his intent with some basic Python functionality and shows how to use various PHP interfaces to achieve a similar functionality. He talks about SPL interfaces like Countable, ArrayAccess and Iterator to make objects more useful in an array handling environment. His example uses the idea of a set of user's tweets (from Twitter) and shows the implementation of these three interfaces.

tagged: array interface countable arrayaccess iterator tutorial

Link: http://www.sitepoint.com/fun-array-interfaces

Jeremy Cook's Blog:
Implementing the ArrayAccess Interface
Feb 02, 2012 @ 13:56:43

Jeremy Cook is back with the next part of his series looking at the handy features PHP's SPL provides. In this new post he looks at the ArrayAccess interface and how it can make your data more accessible to PHP's own array handing functions.

ArrayAccess allows you to treat an object that implements it as if it is an array for the purposes of setting, unsetting and retrieving data from it. Please note the emphasis in the last sentence! ArrayAccess does not make an object behave like an array in any other way. If you pass an object that implements ArrayAccess to a PHP array function such as in_array() you'll still get an error. This will become a little clearer with some of the examples below.

He shows what you'll need to use this interface in your class - implementing the interface and defining a set of four methods to get/set and check for the value in your array. He includes a practical example of pulling data back from an API and wrapping it in a class to make accessing it simpler (also implementing the Countable interface as well, see the previous post for more on that). Code is include to illustrate how it can be used.

tagged: arrayaccess spl tutorial array standardphplibrary countable


Jeremy Cook's Blog:
Using the Countable Interface
Jan 05, 2012 @ 14:39:05

In a recent post to his blog Jeremy Cook has a tutorial about using the Countable interface (part of the SPL) in your objects to make them play nicely with functions like count.

PHP provides a number of predefined interfaces and classes that can really make your life as a developer easier but which are often overlooked. The functionality offered by the Standard PHP Library (SPL) and the predefined interfaces is extremely cool and very powerful but very underutilized. [...] I thought I’d write a few articles with examples of how I’ve used these classes and interfaces in the hope that someone would find it useful. I’d love it if people felt like commenting with their own examples too. I’ll start with a quick look at the Countable interface.

He includes sample code for classes using the Countable interface and defining the custom "count()" method inside. This method lets you define how the object will behave when something like count is called on it. His examples show returning the number of items in a private variable, determining the state of an object and including logic to only find valid data (like from a database table).

tagged: spl standardphplibrary countable interface tutorial


Implementing the ArrayAccess Interface - PHP
Jun 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.

tagged: tutorial arrayaccess interface countable iterator spl


SitePoint PHP Blog:
Sophisticated Object Iterators in PHP
May 05, 2011 @ 12:54:59

Following up on their earlier simple object iterators post, the SitePoint PHP blog is back with a look at more sophisticated iterators you can use to work with database record objects.

In my previous post, Simple Object Iterators in PHP, we discovered how to iterate over array items defined within an object using a foreach loop. However, what if you need to iterate over items which are not stored in an array, e.g. records from a database or lines of text read from a file?

He shows how to create a script that pulls in the users from a database object (PDO, in this case) and implements the Countable and Iterator interfaces. These interfaces give it some special methods that can give counts of the results and help you iterate through the results - current, rewind, next and valid.

tagged: object iterator spl database result pdo countable implement


Havard Eide's Blog:
Aug 01, 2008 @ 10:23:28

In a new post Havard Eide looks at the creation of a Countable interface that can be used in any application:

Today I will look at the Countable interface, it has a single function that needs to be implemented: count(), by implementing this you can ensure that there is a count() function ready to use on any given class that implements it. The Countable interface is used in other places in the SPL as well: the ArrayIterator and ArrayObject classes implements this interface ( and SqliteResult if present ).

In his code examples he shows simple methods for returning the count() of a property, but notes that the real power of it comes in the ability to manipulate the number returned from the call based on other parameters (or filtering).

tagged: countable interface count spl arrayiterator arrayobject