reference appendices

Predefined Classes And Interfaces

PHP ships core classes and interfaces that appear throughout application and library code. Recognising them prevents unnecessary custom abstractions and helps with type declarations.

Use This Reference When

  • Typing collection-like objects with Traversable, Iterator, or Countable.
  • Working with dates, exceptions, closures, generators, and reflection.
  • Reading framework code that implements standard interfaces.

Countable Domain Collection

PHP example
<?php

final class Basket implements Countable
{
    public function __construct(private array $items) {}
    public function count(): int { return count($this->items); }
}

echo count(new Basket(['pen', 'book'])) . PHP_EOL;

// Prints:
// 2

Use the narrowest interface that describes the required behaviour. Do not type against a broad concrete class when callers only need iteration or counting.

Practice

Choose a Standard Interface

Create a small collection that implements Countable. Explain what callers gain from the interface.