Porto

Introduction to SPL ArrayAccess

Introduction to SPL ArrayAccess

Introduction to Standard PHP Library ( SPL )

By Kevin Waterson

Contents

  1. SPL
  2. ArrayAccess
  3. Reflection
  4. Implementation
  5. offsetExists
  6. offsetSet
  7. offsetGet
  8. offsetUnset
  9. Summary
  10. Credits

ArrayAccess

The SPL ArrayAccess interface provides a method of accessing objects as though they were arrays.

Reflection

A full breakdown can be seen with the available methods from the ArrayAccess interface by using the reflection API like this:


<?php
    Reflection
::export(new ReflectionClass('ArrayAccess'));
?>

Now we have a full list of available methods to the ArrayAccess interface. The list will look something like this:

Interface [  interface ArrayAccess ] {
- Constants [0] {
}

- Static properties [0] {
}

- Static methods [0] {
}

- Properties [0] {
- Methods [4] {
Method [  abstract public method offsetExists ] {
- Parameters [1] {
Parameter #0 [  $offset ]
}
}

Method [  abstract public method offsetGet ] {
- Parameters [1] {
Parameter #0 [  $offset ]
}
}

Method [  abstract public method offsetSet ] {
- Parameters [2] {
Parameter #0 [  $offset ]
Parameter #1 [  $value ]
}
}

Method [  abstract public method offsetUnset ] {
- Parameters [1] {
Parameter #0 [  $offset ]
}
}
}
}


Implementation

As the above reflection shows, there are four methods within the ArrayAccess interface. Each of these methods MUST be present in any class that implements ArrayAccess. Failure to implement any of these methods will result in a fatal error. In the following error, a book class was created without the offsetUnset() method and produced the following result.

Fatal error: Class book contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ArrayAccess::offsetUnset) in /www/arrayAccess.php on line 29

When fully implemented with all the required interface methods, the ArrayAccess interface can simple objects into arrays. Lets flesh out the books class and see how it works


<?php
    
class book implements ArrayAccess
    
{
        public 
$title;

        public 
$author;

        public 
$isbn;

        public function 
offsetExists$offset )
        {
            return isset( 
$this->$offset );
        }

        public function 
offsetSet$offset$value)
        {
            
$this->$offset $value;
        }

        public function 
offsetGet$offset )
        {
            return 
$this->$offset;
        }

        public function 
offsetUnset$offset )
        {
            unset( 
$this->$offset );
        }
    } 
/*** end of class ***/

    /*** a new class instance ***/
    
$book = new book;

    
/*** set some book properties ***/
    
$book['title']= 'Pro PHP';
    
$book['author'] = 'Kevin McArthur';
    
$book['isbn'] = 1590598199;

    
print_r($book);
?>
Array
(
	[title] => Pro PHP
	[author] => Kevin McArthur
	[isbn] => 1590598199
)

offsetExists

The offsetExists method checks to see if there is a value for the key specified by the offset, and should return boolean true or false.

offsetSet

This method, as the name implies, sets the offset. Care should be taken here as it has the ability to simply over write any existing property by the same name.

offsetGet

A simple method to get an offset.

offsetUnset

This method is used to remove a value from the array, usually using the php unset() function. Note that if the array is numerically indexed, a call to array_values() will be required to re-index the array if that is the behaviour required.

Summary

The ArrayAccess interface provides a simple and clean method of handling object properties as arrays. This same functionality demonstrated above, could easily be used for the creation of class hierachies etc, and is left to the users imagination.

Credits

Many thanks to Marcus Boerger for his help with the code in this tutorial and quick fixes to bugs in the PHP 5.1.0 core as we came across them. Marcus is a PHP core developer and the creator of SPL. He has provided excellent docs that show the inner workings of SPL at http://www.php.net/~helly/php/ext/spl/.