PHP Doku:: The Countable interface - class.countable.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)InterfacesThe Countable interface

Ein Service von Reinhard Neidl - Webprogrammierung.

Interfaces

<<Interfaces

Countable::count>>


UnterSeiten:

The Countable interface

Einführung

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

Interface-Übersicht

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

Inhaltsverzeichnis


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
isaac dot z dot foster dot nada at spamporfav dot gmail dot com
8.06.2010 10: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
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)