

How to Create a PHP C Extension to Manipulate Arrays Part 2: Adding ArrayAccess and
Aug 13, 2015 @ 12:33:04

Dmitry Mamontov has posted the second part of his "How to Create a PHP C Extension to Manipulate Arrays" series on PHPClasses, building on part one and adding in the ArrayAccess and Traversable interface functionality.

In the first part of this article we learned how to create an extension for PHP written in C to create a class that works like arrays. However, to make the class objects really behave as arrays you need to implement certain interfaces in the class.

Read this article to learn how to make a PHP class defined by a C extension implement ArrayAccess and Traversable interfaces, as well understand how to solve problems that you may encounter that can make your extension slower than you expect.

He takes the class he defined in part one and walks you through the addition of the two interfaces. He shows you where they're defined in the PHP source, what the code looks like and how they integrate with the class. He also shows you how to customize the object class handlers, making it possible to use the custom class (object) as an array. Adding Traversable is easier, adding an iterator return method that allows for the data internal to the class to be iterated through.

tagged: phpclasses series part2 extension class array manipulate arrayaccess traversable

Link: http://www.phpclasses.org/blog/post/306-How-to-Create-a-PHP-C-Extension-to-Manipulate-Arrays-Part-2-Adding-ArrayAccess-and-Traversable-interfaces.html

SitePoint PHP Blog:
Fun with Array Interfaces
Dec 09, 2013 @ 10:53:06




tagged: array interface countable arrayaccess iterator tutorial


Jeremy Cook's Blog:
Implementing the ArrayAccess Interface
Feb 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.

tagged: arrayaccess spl tutorial array standardphplibrary countable


Lorna Mitchell's Blog:
ArrayAccess vs ArrayObject
Sep 16, 2011 @ 09:16:37

Lorna Mitchell has a new post to her blog explaining ArrayObject and ArrayAccess and how each is used.

I help people qualify for Zend Certification and in the last few months I've had questions about both ArrayAccess and ArrayObject. This post is an attempt to illuminate both. In very simple terms, ArrayAccess is an interface, which you can implement in your own objects; ArrayObject, on the other hand, is a class, which you can either use or extend.

She give an example of ArrayAccess - a simple class that implements it to make it work like an array. For ArrayObject, she describes some of the things it comes with, including automatically implementing the ArrayAccess, Countable and Traversable interfaces making it a "more powerful array" type.

tagged: arrayaccess arrayobject interface tutorial


Implementing the ArrayAccess Interface - PHP
Jun 17, 2011 @ 08:58:04

In the first part of a new series over on DevShed.com, they introduce the concept of "segregated interfaces" and show how to use them to work with collections of arrays (using interfaces that are a part of the SPL).

To start illustrating why segregated interfaces are really useful, in the lines to come I’m going to build an example that will recreate the scenario described in the introduction. Basically, what I want to achieve here is to construct a custom countable array collection.

He shows the basic class structure needed to emulate a countable array in an object by implementing the "Countable" interface. He adds in the "Iterator" interface to allow you to work with the dataset like an array - progressing through it, rewinding to the beginning and checking to see if a value exists. Finally, they add the "ArrayAccess" interface to the class that boosts it with even more features like the ability to grab things by specific keys (numeric or string). The finish the article off with an example of an ArrayCollection object and how it can be looped through using a foreach.

tagged: tutorial arrayaccess interface countable iterator spl