Countable::count
(PHP 5 >= 5.1.0)
Countable::count — Count elements of an object
Description
abstract public int Countable::count
([ int
$mode
= COUNT_NORMAL
] )This method is executed when using the count() function on an object implementing Countable.
Parameters
-
mode
-
The optional
mode
parameter will be set toCOUNT_NORMAL
orCOUNT_RECURSIVE
, depending on what value was passed to the second parameter of count(). This is particularly useful for counting all the elements of a multidimensional array/Countable combination.
Changelog
Version | Description |
---|---|
5.6.0 |
The optional mode parameter was added.
|
Examples
Example #1 Countable::count() example
<?php
class myCounter implements Countable {
private $count = 0;
public function count() {
return ++$this->count;
}
}
$counter = new myCounter;
for($i=0; $i<10; ++$i) {
echo "I have been count()ed " . count($counter) . " times\n";
}
?>
The above example will output something similar to:
I have been count()ed 1 times I have been count()ed 2 times I have been count()ed 3 times I have been count()ed 4 times I have been count()ed 5 times I have been count()ed 6 times I have been count()ed 7 times I have been count()ed 8 times I have been count()ed 9 times I have been count()ed 10 times
Коментарии
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) {
}
First, the commonly referenced example of Countable is somewhat misleading from an OOP perspective, as it unintentionally violates some key principles. While it demonstrates the concept of Countable, it does so in a way that could cause confusion.
According to the Countable interface:
<?php
/**
* Count elements of an object
* @link https://php.net/manual/en/countable.count.php
* @return int<0,max> The custom count as an integer.
* <p>
* The return value is cast to an integer.
* </p>
*/
#[TentativeType]
public function count(): int;
?>
This means that the `count()` method should not modify the state of an object—it should simply return an integer value, aligning with SOLID principles.
A proper OOP approach would be:
<?php
class Counter implements Countable
{
private $count = 0;
// Returns the item count of the object
public function count(): int
{
return $this->count;
}
public function increment(): void
{
$this->count++;
}
}
$counter = new Counter;
for ($i = 0; $i < 10; ++$i) {
$counter->increment();
echo "I have been incremented " . count($counter) . " times\n";
}
?>
Here, count() correctly returns the number of elements, while increment() is responsible for modifying the state of the object.
A separate discussion can be had about whether to use count($object) or $object->count(), but that’s a different topic.