Home arrow PHP arrow ArrayObject PHP Class Examples

ArrayObject PHP Class Examples

In this second part of a two-part series on the ArrayObject class from the Standard PHP Library, you'll learn how to handle properties by means of an object syntax.

  1. ArrayObject PHP Class Examples
  2. Building a View Class with PHP ArrayObject
By: Alejandro Gervasio
Rating: starstarstarstarstar / 0
November 16, 2011

print this article



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:


namespace ArrayObject;

$data = array(
    'name'  => 'John Doe',
    'email' => 'john@domain.com'

// create an instance of the ArrayObject class
$arrayObj = new \ArrayObject($data, \ArrayObject::ARRAY_AS_PROPS);

// access the object properties using an object notation
echo 'Full name: ' . $arrayObj->name . ' Email: ' . $arrayObj->email;
// displays the following: Full name: John Doe Email: john@domain.com

// count the number of properties in the object
echo 'Number of properties assigned to the object: ' . count($arrayObj);
// displays the following: Number of properties assigned to the object: 2

// dump the array object
/* displays the following

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
foreach ($arrayObj as $key => $value) {
    echo 'Key: ' . $key . ' Value: ' . $value . '<br />';
/* displays the following

Key: name Value: John Doe
Key: email Value: john@domain.com


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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: