Software Development

How to use the PHP Countable Interface


This article demonstrates how to implement and use the Countable interface in PHP. The Countable interface is normally used in conjunction with other interfaces (i.e., Iterator, ArrayAccess, etc.) to create data structures like PHP SPL Library’s SplDoublyLinkedList and SplStack. Developers can create their own data structures by implementing several of these interfaces. The example on this post is pretty basic. By implementing Countable, we can use the PHP count() function on a Countable object to retrieve the “number of items.”

The Countable interface has one abstract method called count(). Classes that implement this interface must override this method by providing their implementation of count().

Software Requirements

  • Windows 7 Professional SP1
  • PHP 5.5.9 / Zend Engine v2.5.0
  • NetBeans IDE 8.0.1

The Codes

Sample Output

Get the Codes

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like