Home arrow PHP arrow Using External Iterators to Retrieve Data in PHP

Using External Iterators to Retrieve Data in PHP

In this final part of our series on external iterators (loops) in PHP, we will look at how to retrieve data using the ArrayIterator SPL class.

TABLE OF CONTENTS:
  1. Using External Iterators to Retrieve Data in PHP
  2. Save and Fetch Data from Files in PHP
  3. PHP ArrayIterator SPL Class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
April 13, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In most cases the implementation of external iterators in PHP takes place when it becomes necessary to build wrappers around database result sets and plain arrays. However, they can be of great help when it comes to sticking with the DRY (Donít Repeat Yourself) and the Single Responsibility principles as well. By using an outer element that encapsulates an intuitive API and all the functionality required for iterating over common data structures like the ones mentioned above, it is possible to build different types of traversable classes without having to pollute them with unnecessary responsibilities.

To demonstrate how to translate these seemingly-complex theoretical concepts into concrete and functional code, in earlier chapters of this series I developed a few examples which showed how to use external iterators; first for traversing the protected fields of an entity modeling class and secondly for constructing a file helper, which had the ability for lazy-loading data from a sample text file. This last example is especially interesting, as its driving logic can be adapted to work with other types of persistence mechanisms, such a RDBMS, a web service or a CDN.

In addition, itís valid to point out that the aforementioned file helper made use of a custom array collection to fetch (on request) data from a specified target file. Considering that the data was converted to an array prior to being processed, itíd be really easy to refactor the helper and make it work with the native ArrayIterator SPL class. That would mean writing less code, while maintaining the same functionality. A hard-to-beat deal, indeed.

In line with this idea, in this final installment of the series Iím going to show you how simple is to refactor the pertinent file helper, so that it can employ the built-in ArrayIterator class for lazy-loading file data, instead of appealing to a custom array collection.

Recap Time: Lazy-loading File Data Using a Custom External Iterator

In case that you still havenít had the chance to read the preceding tutorials in this series, I will recap the source code corresponding to this example, so that you can dissect it and grasp its driving logic.

With that being said, hereís an implementation of the pertaining external iterator. Take a look:

(Utility/ArrayCollection.php)

<?php

namespace Utility;

class ArrayCollection implements \Countable, \Iterator, \ArrayAccess
{
    protected $_data = array();
   
    /**
     * Constructor
     */
    public function __construct(array $data = array())
    {
        if (!empty($data)) {
            $this->_data = $data;
        }
        $this->rewind();
    }
   
    /**
     * Count the number of elements in the collection (implementation required by Countable interface)
     */
    public function count()
    {
        return count($this->_data);
    }
   
    /**
     * Reset the collection (implementation required by Iterator interface)
     */    
    public function rewind()
    {
        reset($this->_data);
    }
   
    /**
     * Get the key of the current element in the collection (implementation required by Iterator interface)
     */
    public function key()
    {
        return key($this->_data);
    }
   
    /**
     * Get the current element in the collection (implementation required by Iterator interface)
     */
    public function current()
    {
        return current($this->_data);
    }

    /**
     * Check if there're more elements in the collection (implementation required by Iterator Interface)
     */
   
    public function valid()
    {
        return ($this->current() !== false);
    }
   
    /**
     * Move to the next element in the collection (implementation required by Iterator interface)
     */
    public function next()
    {
        next($this->_data);
    }
   
    /**
     * Add an element to the collection (implementation required by ArrayAccess interface)
     */
    public function offsetSet($key, $value)
    {
        if (!isset($key)) {
            $this->_data[] = $value;
        }
        else {
            $this->_data[$key] = $value;
        }
        return true;
    }
   
    /**
     * Remove an element from the collection (implementation required by ArrayAccess interface)
     */
    public function offsetUnset($key)
    {
        if (isset($this->_data[$key])) {
            unset($this->_data[$key]);
            return true;
        }
        return false;
    }
   
    /**
     * Get the specified element in the collection (implementation required by ArrayAccess interface)
     */
    public function offsetGet($key)
    {
        return isset($this->_data[$key]) ?
               $this->_data[$key] :
               null;
    } 
   
    /**
     * Check if the specified element exists in the collection (implementation required by ArrayAccess interface)
     */    
    public function offsetExists($key)
    {
        return isset($this->_data[$key]);
    }

    /**
     * Return the elements in the collection
     */
    public function toArray()
    {
        return $this->_data;
    }
}

As you can see, the ďArrayCollectionĒ class is nothing but a generic iterator, which enables it to traverse, count and access the elements stored in its protected $_data field by using an array-like notation.



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