I’m pretty sure that you now know how to do simple things with the ArrayObject class, like assigning some properties to it which can be manipulated via an array notation. It’s valid to point out that the class allows you to treat the properties using an object-oriented syntax by passing an additional constant (or flag) called ARRAY_AS_PROPS to the class’s constructor. This scenario will be covered in the second part of this tutorial. However, if you’re wondering how the object currently handles the properties as if they were array elements, the answer is simple: it uses by default a flag named STD_PROP_LIST, which gives the class this array-like feeling. Given that, the first example could be rewritten as follows: <?php namespace ArrayObject; $data = array( // 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 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 */ As I just explained, the ArrayObject’s default behavior is controlled by the STD_PROP_LIST constant, which allows properties to be manipulated as common array elements. Of course, it’s possible to assign those properties after the object has been created, as shown by the following code snippet: <?php namespace ArrayObject; $data = array( // create an instance of the ArrayObject class $arrayObj['name'] = 'John Doe'; // access the object properties using an array notation // count the number of properties in the object Regardless, it’s fairly simple to recreate part of this functionality via custom implementations. A clear example of this is the Zend_Registry component, which is a subclass of ArrayObject (http://framework.zend.com/manual/en/zend.registry.using.html). Therefore, if you found the class’s functionality appealing enough, you might want to further extend it by using Inheritance. Closing Remarks In this introductory part of a two-part tutorial, I developed a few approachable examples to show you how simple it is to get things up and running with the ArrayObject SPL class. As I stated previously, it permits you to easily access properties through an array syntax. It's also feasible to change this behavior and make it treat the pertinent properties using an object notation. That’s precisely the topic that I’ll be discussing in detail in the last installment. You won't want to miss it.
blog comments powered by Disqus |
|
|
|
|
|
|
|