Added to PHP5 and improved in successive releases, the Standard PHP Library (SPL) is one of those things that, once you’ve used it, makes you wonder how you could live so long (and so painfully) without it. The library provides, right out the box, a set of native interfaces and classes which allow you to perform different tasks, without having to enter "userland." While the SPL's core functionality is unquestionably hard to beat, sometimes it's a little overwhelming; it includes so many classes, and some of them have been assigned some strange names (especially the native iterators). All jokes aside, the truth is that some people feel a bit reluctant to explore the SPL in depth; thus, they miss some “goodies” hidden in the library. A glaring example of this is the ArrayObject class. This class is an iteratable and countable structure which makes it possible to treat objects as arrays. In fact, ArrayObject has spread to several popular frameworks and libraries. The class is flexible enough to be used as a multipurpose base implementation, which in most cases is refined via Inheritance. Despite this, it remains as an overlooked SPL component that deserves a closer analysis. Therefore, in this two-part tutorial I’ll be creating some examples to show you how to get things rolling with the ArrayObject class (at least at a basic level), and how to put it to work in a pretty realistic use case. Now that we're doing with the preliminaries, let's get to work. ArrayObject Class: Assigning Properties, Looping and More As I noted in the introduction, one of ArrayObject's virtues is its flexibility, as it will let you pass to its constructor either an array of values which are stored in private properties, or an object. To make things easier to follow, let me show you how to take the first path. In the simplest use case, creating an instance of the class, and injecting a trivial array into the constructor can be accomplished in the following way (the specification of a namespace is, of course, optional): <?php namespace ArrayObject; $data = array( // create an instance of the ArrayObject class Definitely, not much needs to be said about the above snippet, as it speaks for itself. So, let’s move on and see how easy it is to manipulate the data just passed to the ArrayObject. The code fragment below does exactly that. Check it out: // access the object properties using an array 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" } } // put the array object in a foreach loop Key: name Value: John Doe */ Now, things are getting a bit more interesting. As you can see, it’s simple to retrieve and count the properties assigned to the ArrayObject, as well as and traverse the object through a “foreach” loop. It's also possible to feed the object in question another one, by passing it to the constructor. If you’re curious and want to see how to do that, simply look at the following code fragment. It defines a contrived “User” class: (User.php) <?php namespace ArrayObject; class User With this sample class already set (notice that its properties have been deliberately declared public), it’s really simple to pass an instance of it to the constructor of the ArrayObject and perform the same tasks that you saw in the earlier array-based example. Again, be sure to check the following snippet: <?php
require_once 'User.php'; // create an instance of the User class // create an instance of the ArrayObject class // access the object properties using an array notation // count the number of properties in the object // dump the array object bject(ArrayObject)#2 (1) { ["storage":"ArrayObject":private]=> object(ArrayObject\User1)#1 (2) { ["_name"]=> string(8) "John Doe" ["_email"]=> string(15) "john@domain.com" } } */ Key: _name Value: John Doe */ That was pretty easy to grasp, wasn’t it? In this case, the properties of the “User” class remained accessible to the ArrayObject, as they were defined public, thus making it possible to count them, drop them into a “foreach” loop and so forth. If they were protected or private, though, the object wouldn’t be able to retrieve them, and consequently, the PHP interpreter would trigger a few ugly notices. Just try this for yourself to grasp in a snap the inner workings of this process.
blog comments powered by Disqus |
|
|
|
|
|
|
|