Home arrow PHP arrow Page 2 - ArrayObject PHP Class: An Introduction

Handling Properties As Array Elements - PHP

In this first part of a two-part tutorial, I demonstrate how simple it is to use the ArrayObject SPL class. This class permits you to easily access properties through an array syntax.

TABLE OF CONTENTS:
  1. ArrayObject PHP Class: An Introduction
  2. Handling Properties As Array Elements
By: Alejandro Gervasio
Rating: starstarstarstarstar / 0
November 16, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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(
    'name'  => 'John Doe',
    'email' => 'john@domain.com'
);

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

// access the object properties using an array 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
var_dump($arrayObj);
/* 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

*/

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(
    'name'  => 'John Doe',
    'email' => 'john@domain.com'
);

// create an instance of the ArrayObject class
$arrayObj = new \ArrayObject;
$arrayObj->setFlags(\ArrayObject::STD_PROP_LIST);

$arrayObj['name'] = 'John Doe';
$arrayObj['email'] = 'john@domain.com';

// access the object properties using an array 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 
   
There you have it. At this point, you should have a clearer idea of how to use the ArrayObject SPL class to perform a few basic tasks. Unfortunately, its major drawback is that it canít be used with most of the built-in PHP functions. Sad but true.

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: