Home | MySQL Russian Manual | MySQL Manual | Apache HTTP Server Rus Documentation | Apache HTTP Server Documentation | downloads | faq

search for in the  Language: Russian


The Countable interface

Introduction

Classes implementing Countable can be used with the count() function.

Interface synopsis

Countable
Countable {
/* Methods */
abstract public int count ( void )
}

Table of Contents


User Contributed Notes
Countable
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
?>
 

 
credits | contact