downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Countable::count> <Interfaces
Last updated: Fri, 20 May 2011

view this page in

El interfaz Countable

Introducción

Las clases implementado Countable pueden ser usadas con la función count().

Interfaz synopsis

Countable {
/* Métodos */
abstract public int count ( void )
}

Table of Contents



add a note add a note User Contributed Notes Countable
Anonymous 17-Jan-2011 02:10
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.
isaac dot z dot foster dot nada at spamporfav dot gmail dot com 08-Jun-2010 08:18
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
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites