News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Jeremy Cook's Blog:
Using the Countable Interface
January 05, 2012 @ 14:39:05

In a recent post to his blog Jeremy Cook has a tutorial about using the Countable interface (part of the SPL) in your objects to make them play nicely with functions like count.

PHP provides a number of predefined interfaces and classes that can really make your life as a developer easier but which are often overlooked. The functionality offered by the Standard PHP Library (SPL) and the predefined interfaces is extremely cool and very powerful but very underutilized. [...] I thought I'd write a few articles with examples of how I've used these classes and interfaces in the hope that someone would find it useful. I'd love it if people felt like commenting with their own examples too. I'll start with a quick look at the Countable interface.

He includes sample code for classes using the Countable interface and defining the custom "count()" method inside. This method lets you define how the object will behave when something like count is called on it. His examples show returning the number of items in a private variable, determining the state of an object and including logic to only find valid data (like from a database table).

0 comments voice your opinion now!
spl standardphplibrary countable interface tutorial


blog comments powered by Disqus

Similar Posts

Robert Basic: Working with custom view helpers in Zend Framework 2

Jim Plush\'s Blog: My-Bic Ajax Framework Video Tutorials Online

Cormac's Blog: Fast server-side rejection of large image uploads using $_FILES

Matthew Weir O'Phinney's Blog: Cgiapp2 Tutorial 1 - Switch Template Plugins at Will

Mpwebwizard.com: Use a mirror for hits from popular websites


Community Events





Don't see your event here?
Let us know!


list framework developer testing refactor code unittest release install community series interview experience introduction threedevsandamaybe opinion language podcast laravel symfony2

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework