note 98303 added to class.countable

From: Date: Tue, 08 Jun 2010 08:18:36 +0000
Subject: note 98303 added to class.countable
Groups: php.notes 
Request: Send a blank email to php-notes+get-169932@lists.php.net to get a copy of this message
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 :) ) //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 ---- Server IP: 69.147.83.197 Probable Submitter: 71.190.79.99 ---- Manual Page -- http://www.php.net/manual/en/class.countable.php Edit -- https://master.php.net/note/edit/98303 Del: integrated -- https://master.php.net/note/delete/98303/integrated Del: useless -- https://master.php.net/note/delete/98303/useless Del: bad code -- https://master.php.net/note/delete/98303/bad+code Del: spam -- https://master.php.net/note/delete/98303/spam Del: non-english -- https://master.php.net/note/delete/98303/non-english Del: in docs -- https://master.php.net/note/delete/98303/in+docs Del: other reasons-- https://master.php.net/note/delete/98303 Reject -- https://master.php.net/note/reject/98303 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169932) next »