Countable::count

(PHP 5 >= 5.1.0, PHP 7)

Countable::countZlicza liczbę elementów obiektu

Opis

abstract public Countable::count ( void ) : int

Ta metoda jest wywoływana kiedy używana jest funkcja count() na obiekcie implementującym Countable.

Parametry

Ta funkcja nie posiada parametrów.

Zwracane wartości

Własna ilość jako integer.

Informacja:

Zwracana wartość jest rzutowana na integer.

Przykłady

Przykład #1 Przykład 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 
"Zostałem zliczony " count($counter) . " razy\n";
}
?>

Powyższy przykład wyświetli coś podobnego do:

Zostałem zliczony 1 razy
Zostałem zliczony 2 razy
Zostałem zliczony 3 razy
Zostałem zliczony 4 razy
Zostałem zliczony 5 razy
Zostałem zliczony 6 razy
Zostałem zliczony 7 razy
Zostałem zliczony 8 razy
Zostałem zliczony 9 razy
Zostałem zliczony 10 razy

add a note add a note

User Contributed Notes 1 note

up
11
SenseException
10 years ago
Even though Countable::count method is called when the object implementing Countable is used in count() function, the second parameter of count, $mode, has no influence to your class method.

$mode is not passed to  Countable::count:

<?php

class Foo implements Countable
{
    public function
count()
    {
       
var_dump(func_get_args());
        return
1;
    }
}

count(new Foo(), COUNT_RECURSIVE);

?>

var_dump output:

array(0) {
}
To Top