array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'fr', ), 'this' => array ( 0 => 'countable.count.php', 1 => 'Countable::count', ), 'up' => array ( 0 => 'class.countable.php', 1 => 'Countable', ), 'prev' => array ( 0 => 'class.countable.php', 1 => 'Countable', ), 'next' => array ( 0 => 'class.outeriterator.php', 1 => 'OuterIterator', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'fr', 'path' => 'reference/spl/countable/count.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
Countable::count — Compte le nombre d'éléments d'un objet
Countable::count() est exécutée lorsque la fonction count() est appelée sur un objet qui implémente l'interface Countable.
Cette fonction ne contient aucun paramètre.
Exemple #1 Exemple avec Countable::count()
<?php
class myCounter implements Countable {
private $count = 0;
public function count() {
return ++$this->count;
}
}
$counter = new myCounter;
for($i=0; $i<10; ++$i) {
echo "J'ai été compté " . count($counter) . " fois\n";
}
?>
Résultat de l'exemple ci-dessus est similaire à :
J'ai été compté 1 fois J'ai été compté 2 fois J'ai été compté 3 fois J'ai été compté 4 fois J'ai été compté 5 fois J'ai été compté 6 fois J'ai été compté 7 fois J'ai été compté 8 fois J'ai été compté 9 fois J'ai été compté 10 fois