Countable

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 )

First we’ll look at a simple example that keeps track of my sites:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
class WebSiteCollection implements Countable {
    private $sites;
 
    public function __construct() {
        $this->sites = array( 
        	"http://eide.org",
        	"http://phpvolcano.com"
        );
    }
    public function count() {
        return count( $this->sites );
    }
}
$sites = new WebSiteCollection();
echo "I have " . $sites->count() . " sites";
?>

Running this you will get:
I have 2 sites
As you can see: we call count() on the object and we get how many sites I have running, but: that is not the good thing with Countable: it also hooks into the native count() functionality so that you can do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
class WebSiteCollection implements Countable {
    private $sites;
 
    public function __construct() {
        $this->sites = array( 
        	"http://eide.org",
        	"http://phpvolcano.com"
        );
    }
    public function count() {
        return count( $this->sites );
    }
}
$sites = new WebSiteCollection();
 
echo "I have " . count( $sites ) . " sites";
?>

Which will output the same as the first example.

The above examples are very basics, but the power of Countable also lies in the fact that you can manipulate the count within the count() function before returning it, making it respond to a certain flag like the following example where we can set a TLD that we want the count() function to restrict to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
class WebSiteCollection implements Countable {
    private $sites;
    private $tldrestriction;
 
    public function __construct() {
        $this->sites = array( 
        	"http://eide.org",
        	"http://phpvolcano.com"
        );
        $this->tldrestriction = null;
    }
 
    public function restrictToTld( $tld ) {
    	$this->tldrestriction = $tld;
    }
 
    public function count() {
        if( null === $this->tldrestriction ) {
    		return count( $this->sites );
        }
        $count = 0;
        foreach( $this->sites as $site ) {
        	if( preg_match( "/".$this->tldrestriction."$/", $site ) ) {
        		$count++;
        	}
        }
        return $count;
    }
}
$sites = new WebSiteCollection();
 
echo "I have " . count( $sites ) . " sites\n";
$sites->restrictToTld( "org" );
echo "I have " . count( $sites ) . " sites\n";

And the output for this will be:
I have 2 sites
I have 1 sites

This has been a short post, but the Countable interface is really easy to use and there is as said only a single function to implement, and by using it you can ensure that your class correctly will return the current number of elements when you start to filter out elements and such.

3 comments to “Countable”

You can leave a reply or Trackback this post.
  1. This is a nice writeup. Thanks for the information I am sure many will benefit from this.

  2. Thanks for this post. very usefull.

Write a Reply or Comment

Your email address will not be published.