Software Development

How to use the PHP Countable Interface



This article demonstrates how implement and use the Countable interface in PHP. The Countable interface is normally used in conjunction with other interfaces (i.e., Iterator, ArrayAccess and 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” in it.

The Countable interface has one abstract method called count(). Classes that implements this interface must override this method by providing their own 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


You Might Also Like