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:
Implementing the ArrayAccess Interface
February 02, 2012 @ 13:56:43

Jeremy Cook is back with the next part of his series looking at the handy features PHP's SPL provides. In this new post he looks at the ArrayAccess interface and how it can make your data more accessible to PHP's own array handing functions.

ArrayAccess allows you to treat an object that implements it as if it is an array for the purposes of setting, unsetting and retrieving data from it. Please note the emphasis in the last sentence! ArrayAccess does not make an object behave like an array in any other way. If you pass an object that implements ArrayAccess to a PHP array function such as in_array() you'll still get an error. This will become a little clearer with some of the examples below.

He shows what you'll need to use this interface in your class - implementing the interface and defining a set of four methods to get/set and check for the value in your array. He includes a practical example of pulling data back from an API and wrapping it in a class to make accessing it simpler (also implementing the Countable interface as well, see the previous post for more on that). Code is include to illustrate how it can be used.

0 comments voice your opinion now!
arrayaccess spl tutorial array standardphplibrary countable


blog comments powered by Disqus

Similar Posts

Matthew Weier O'Phinney's Blog: Creating Re-Usable Zend_Application Resource Plugins

Zend Developer Zone: The Standard PHP Library (SPL)

Felix Geisendorfer's Blog: Model::save() now returns an array!

PHPBuilder.com: Create a Unit Conversion Application w/the Zend_Measure Component

The Bakery: New Tutorials - PHPTAL, Multiple Checkboxes, and counterCache


Community Events





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


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

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