Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Countable Interface

The Countable interface is a native built-in interface that allows objects to be counted using the count() function.

By implementing the Countable interface, a class defines custom logic for determining its count.

<?php
class Collection implements Countable
{
    private $list = [];
    
    public function add($tusk) {
        $this->list[] = $tusk;
    }

    public function count()
    {
        return $this->count;
    }

}

$count = new Counter;
$count->inc();
$count->inc();

print count($count).PHP_EOL;

?>

Documentation

See Also