Home arrow PHP arrow Page 3 - Using External Iterators to Retrieve Data in PHP

PHP ArrayIterator SPL Class - 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

Refactoring the File Helper: Using the Native ArrayIterator SPL Class

To be frank, refactoring the prior file helper so that it can use the SPL ArrayIterator class instead of a custom iterator is a straightforward process reduced to amending its “getIterator()” method and nothing more. If this isn’t clear enough for you, pay attention to the following code sample, which shows the improved version of the helper:

(Utility/FileProcessor.php)

<?php

namespace Utility;

class FileProcessor implements \IteratorAggregate
{
    protected $_file = 'Utility\data.txt';
    protected $_iterator;

    /**
     * Constructor
     */
    public function __construct($file = '')
    {
        if ($file !== '') {
            $this->setFile($file);
        }
    }

    /**
     * Set the target file
     */
    public function setFile($file)
    {
        if (!file_exists($file)) {
            throw new \InvalidArgumentException('The target file ' . $file . ' does not exist.');
        }
        $this->_file = $file;
    }

    /**
     * Get the target file
     */
    public function getFile()
    {
        return $this->_file;
    }

    /**
     * Write the specified data to the target file
     */
    public function write($data)
    {
        if (!$fp = fopen($this->_file, 'a+')) {
            throw new \UnexpectedValueException('Error opening the target file.');
        }
        if (!fwrite($fp, $data . "\n")) {
            throw new \UnexpectedValueException('Error writing data to the target file.');
        }
        fclose($fp);
        return $this;
    }

    /**
     * Get the external iterator
     */
    public function getIterator()
    {
        if ($this->_iterator === null) {
            if (($data = file_get_contents($this->_file)) !== false) {
               $data = explode("\n", $data);
               $this->_iterator = new \ArrayIterator($data);
            }
            else {
                throw new \UnexpectedValueException('Error reading data from the target file.'); 
            }
        }
        return $this->_iterator;
    }
}

Mission accomplished, at least for the moment. As you can see, the file helper is still capable of lazy-loading data from the target file, only now it performs this task via the native ArrayIterator class. With this subtle, yet useful change already implemented, the only thing that needs to be done is set up an example that shows the functionality of the file helper in its “renewed” state.

Setting Up a Final Example: Putting the File Helper to Work

As you may already guessed, testing the revamped version of the file helper doesn’t require us to alter any section of the client code that consumes it, as the substitution of a custom external iterator for a native one has been neatly shielded from the outside.

The following script shows this in a nutshell. Check it out: 

<?php

use Utility\FileProcessor as FileProcessor;

// include the autoloader
require_once 'Autoloader.php';
Autoloader::getInstance();

// create an instance of the FileProcessor class
$fileProc = new FileProcessor;

// write some data to the target file

$fileProc->write('This is the first line of the file')
         ->write('This is the second line of the file')
         ->write('This is the third line of the file');

// lazy-load file data
foreach ($fileProc as $key => $value) {
    echo $value . '<br />';
}

/* displays the following

This is the first line of the file
This is the second line of the file
This is the third line of the file

*/

Done. At this point, I managed to create a file helper capable of lazy-loading data, which performs this task thanks to the use of a built-in external iterator. Of course, in the real world things are not so easy as they are in the above example, which means that in many cases it’s necessary to implement a custom iterator.

Nevertheless, if you’re planning to build an application that, among other things, will traverse, count and access a lot of arrays, the native ArrayIterator class encapsulates enough functionality to get the job nicely done. So, keep your eyes on it in your next PHP project.     

Final Thoughts

Finally, we’ve come to the end of this hopefully educational journey on building external iterators in PHP. Armed with the examples that I provided you in the subsequent tutorials, at this stage you should be capable of building your own external iterators with minor hassles. And bear in mind that, no matter if you need to iterate over database result sets, class data members or simply plain vanilla arrays, external iterators are useful structures that will let you build classes that have a fewer range of responsibilities.

See you in the next PHP tutorial!

 



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