As one of the building blocks of the Standard PHP Library (SPL), the ArrayObject class is a traversable, countable structure, which allows you to treat objects as arrays. This functionality seems irrelevant at first sight, but the class may be a real time-saver in certain situations. That's especially true when you need to create custom implementations (for instance, a dynamic registry or a service locator) without having to implement from scratch the ArrayAccess, IteratorAggregate and Countable SPL interfaces. The best way to get things rolling with ArrayObject is by example. In the first part of this tutorial I created several examples that demonstrated how to assign, count and traverse the properties assigned to some instances of this class by using an array notation. Even though the examples were pretty contrived, they illustrated how versatile the class can be in a decent variety of situations. It’s valid to point out, though, that ArrayObject's abilities aren’t limited to handling properties as if they were array elements. When the constant (or flag, in the class’s jargon) ARRAY_AS_PROPS is passed to the constructor or to the “setFlags()” method, it’s possible to handle the properties by means of an object syntax. Given that, in this last part of the tutorial I’ll be showing you how to accomplish this. Not only that, but if you wish to see how to use ArrayObject in a more realistic case, I’ll be constructing an extendable view class, which will use ArrayObject's functionality as the base implementation. Now let's get started. Using the ArrayObject Class: Assigning Properties by Using an Object Notation As I just explained, it’s really simple to instruct the ArrayObject class to handle its properties in an object-based manner, instead of as array elements. Getting this behavior is as easy as passing the constant ARRAY_AS_PROPS to the class’s constructor. As usual, the most instructive way to grasp the logic of this process is by examining a concrete code sample. Therefore, be sure to take a peek at the one below, which hopefully will be quite illustrative: <?php namespace ArrayObject; $data = array( // create an instance of the ArrayObject class // access the object properties using an object notation // count the number of properties in the object // dump the array object object(ArrayObject)#1 (1) { ["storage":"ArrayObject":private]=> array(2) { ["name"]=> string(8) "John Doe" ["email"]=> string(15) "john@domain.com" } } */ Key: name Value: John Doe */ Definitely, you shouldn’t have any trouble understanding how the script above does its business. It's pretty similar to those you saw in the previous article. In this case, though, things are slightly different; the aforementioned ARRAY_AS_PROPS flag has been passed to the ArrayObject class upon construction. This minor modification allows it to access the properties assigned to it by using an object notation. Please be aware that the behavior of the class when using the ARRAY_AS_PROPS flag might be a little quirky, depending on the PHP version that you’re using. So, if you notice this issue, make sure to update your PHP installation to the latest release. Having clarified that point, now that you've learned how to massage the ArrayObject class to behave like a “true” object, it’s time to show off the class’s functionality in a more realistic use case. In the next section I’ll be building a simple view class which will be an extension of ArrayObject.
blog comments powered by Disqus |
|
|
|
|
|
|
|