Home arrow PHP arrow Implementing the Countable SPL Interface

Implementing the Countable SPL Interface

In this third part of a seven-part series on the Iterator, Countable and ArrayAccess SPL interfaces, I give the MySQLi_ResultWrapper class developed in the previous part the capability of counting the number of rows contained in a given result set. I'll do that by implementing the "count()” method declared by the Countable interface. Doing this will make it possible to use an instance of the class to traverse data sets and count records as if they were plain PHP arrays, which is quite appealing to client code that works with the class’s API.

  1. Implementing the Countable SPL Interface
  2. Review: implementing the Iterator interface
  3. Counting rows in result sets with the Countable SPL interface
  4. The full source code for the MySQLi_ResultWrapper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 30, 2010

print this article



As you possibly know, the Standard PHP Library (also know simply by its acronym, SPL) is comprised of a powerful set of functions, classes and interfaces that permit developers to address problems that occur frequently during the development of web programs. Ranging from autoloading classes on demand and overloading arrays, to recursively traversing directories in the file system, the SPL makes all of these tasks painless.

Among the interfaces included with the library, there are a few that deserve a closer analysis. They can be used to construct classes that allow you to treat distinct data collections as plain PHP arrays. Yes, as this article’s title suggests, in this case I’m talking specifically about the Iterator, Countable and ArrayAccess interfaces. When properly implemented by a class, these interfaces provide it with an intuitive API that can be employed to manipulate text files, database result sets, and so forth by using an array-like notation.

To demonstrate how useful these interfaces can be in a concrete situation, in the two previous parts of this series, I started building a couple of MySQL abstraction classes that performed some common tasks, such as connecting to the database server and running queries, fetching rows and iterating over data sets. I made the last of these classes an implementer of the SPL Iterator interface, which gave it the ability to traverse result sets by way of a simple “foreach()” construct, thanks the implementation of the “rewind(),” “current(),” “key(),” “valid()” and “next()” methods declared by the interface in question.

Although the class in its current state is pretty functional, it lacks an important feature -- it doesn’t implement a method that counts the number of records contained in a given result set. To address this issue, in this third installment of the series I’m going to show you how to make the class an implementer of the Countable interface. Doing this will provide the class with a “count()” method, which naturally will be used for performing the aforementioned counting task in data sets.

Ready to learn a few more useful things about the Countable SPL interface? Then, go ahead and begin reading right away!            

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: