略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Countable

2024-11-23

Countable 接口

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

简介

类实现 Countable 可被用于 count() 函数.

接口摘要

interface Countable {
/* 方法 */
abstract public count(): int
}

目录

add a noteadd a note

User Contributed Notes 4 notes

up
48
isaac dot z dot foster dot nada at spamporfav dot gmail dot com
12 years ago
I just want to point out that your class has to actually implement the Countable interface, not just define a count method, to be able to use count($object) and get the expected results. I.e. the first example below won't work as expected, the second will. (The normal arrow function accessor ($object->count()) will work fine, but that's not the kewl part :) )

<?php
//Example One, BAD :(

class CountMe
{

    protected
$_myCount = 3;

    public function
count()
    {
        return
$this->_myCount;
    }

}

$countable = new CountMe();
echo
count($countable); //result is "1", not as expected

//Example Two, GOOD :)

class CountMe implements Countable
{

    protected
$_myCount = 3;

    public function
count()
    {
        return
$this->_myCount;
    }

}

$countable = new CountMe();
echo
count($countable); //result is "3" as expected
?>
up
9
Rudiger
2 years ago
Unlike an array, an object of a class that implements Countable and has 0 elements is not considered empty:

class C implements Countable {
    public function count() {
        return 0;
    }
}

$a = [];
var_dump($a);
echo 'array is empty: '; var_dump(empty($a));

$c = new C;
var_dump($c);
echo 'Countable is empty: ' ; var_dump(empty($c));

Output:
array(0) {
}
array is empty: bool(true)
object(C)#1 (0) {
}
Countable is empty: bool(false)
up
5
adam at adamhahn dot com
5 years ago
When using GMP/BC/Floating-Point Numbers to work with integers larger than PHP_INT_MAX, be aware that using the count() function will typecast the returned value to an integer.

<?php
class counter implements Countable {
    public function
count() {
       
// Number of IPv6 addresses in a single /32 IPv6 allocation (2^96)
       
return "18446744073709551616"; // assume generated/exported by big-int library(GMP/BC/etc.)
   
}
}

$obj = new counter();

echo
$obj->count(); // prints string "18446744073709551616"
echo count($obj);    // prints int PHP_INT_MAX

// This is because of the typecasting
echo (int) "18446744073709551616"; // prints int PHP_INT_MAX
?>

This will also cause problems for floating-point values.

<?php
class counter implements Countable {
    public function
count() {
       
// Number of IPv6 addresses in a single /32 IPv6 allocation (2^96)
       
return 18446744073709551616;
    }
}

$obj = new counter();

echo
$obj->count(); // prints float 18446744073709551616.000000
echo count($obj);    // prints int 0

// This is because of the typecasting
echo (int) 18446744073709551616; // prints int 0
?>

This is only problematic when counting higher than PHP_INT_MAX.
up
2
Anonymous
11 years ago
Note that arrays don't implement countable. Therefore you can't force a countable parameter for a function if you want it also to work with native arrays.

官方地址:https://www.php.net/manual/en/class.countable.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3