Home arrow PHP arrow Page 2 - Segregated Interfaces in PHP

Building an Array Collection in PHP - PHP

In this first part of a programming series, you will learn how to create segregated interfaces to perform specific tasks in PHP. In this instance, you will be using them to iterate through arrays.

TABLE OF CONTENTS:
  1. Segregated Interfaces in PHP
  2. Building an Array Collection in PHP
  3. Implementing the ArrayAccess Interface
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
June 15, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement


Getting started: building an array collection

To start illustrating why segregated interfaces are really useful, in the lines to come I’m going to build an example that will recreate the scenario described in the introduction. Basically, what I want to achieve here is to construct a custom countable array collection.

At this first stage, the collection will only be capable of counting the elements added to it and nothing else; it’ll use only the contract declared by the “Countable” PHP interface. Based on this, here’s how this sample collection looks:


(ArrayCollection.php)

class ArrayCollection implements Countable
{
    protected $_data = array();
   
    /**
     * Constructor
     */
    public function __construct(array $data = array())
    {
        if (!empty($data)) {
            $this->_data = $data;
        }
    }
   
    /**
     * Count the number of elements in the collection (implementation required by Countable interface)
     */
    public function count()
    {
        return count($this->_data);
    }
}

As you can see, the functionality of the above “ArrayCollection” class is pretty limited. However, this has been done deliberately, as the only thing that the class needs to do, at least for now, is to count its inputted elements.

In keeping with this, the collection only implements the “Countable” native interface, which (not surprisingly) turns out to be a segregated interface, as it only provides its implementer with the functionality that it requires.

Next it’s time to add one more to the earlier array collection. Considering that this class must also be capable of traversing its internal elements, it must then implement the “Iterator” PHP interface.

Making the collection iterable: implementing the “Iterator” interface

The next step that must be taken is to provide the previous array collection with the ability to traverse the elements added to it. Of course, this can be accomplished using another segregated interface, which turns out to the built-in “Iterator”.

Having said that, now check the following code sample, which shows the results of this implementation process:

(ArrayCollection.php)class ArrayCollection implements Countable, Iterator
{
    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);
    }
}

Thanks to the use of another segregated interface, the previous array collection class is capable of iterating over its inputted elements by using a “foreach” construct.

Taking into account that this is a straightforward process, which you’ve probably tackled many times before when you wrote your own custom iterators, it’s time to move forward and provide the earlier array collection with the ability for adding, removing and accessing its internal elements. Well, not surprisingly this enhancement procedure can be easily achieved by making the class an implementer of the “ArrayAccess” native interface.



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