Start · Sprachen · PHP · Referenz · Countable

Countable

Klasse

Interface mit einer Methode <code>count()</code>. Erlaubt <code>count($obj)</code> auf beliebigen Objekten.

seit PHP 5.1.0 Kategorie: oop

Signatur

interface Countable

Beschreibung

Standard bei allen Collection-Klassen.

Rückgabewert

Typ
void

Beispiele

Basis

<?php

class Cart implements Countable {
    public function __construct(private array $items = []) {}
    public function count(): int { return count($this->items); }
}

echo count(new Cart([1, 2, 3])); // 3