Home arrow PHP arrow Page 2 - Using Restrictive Constructors in PHP 5

Building a generic data iterator class with a restrictive constructor - PHP

What exactly is a restrictive constructor, and why would you want to use one? If you're a PHP programmer who uses such design patterns as Singleton and Factory, this series of articles on restrictive constructors will give you another tool to use in your applications.

TABLE OF CONTENTS:
  1. Using Restrictive Constructors in PHP 5
  2. Building a generic data iterator class with a restrictive constructor
  3. Building a basic file iterator
  4. Traversing a simple text file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 04, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

In order to illustrate a basic usage of a restrictive constructor, in the following lines I'm going to define a generic class that will permit you to iterate over an array through a very simple API. Of course, before I hear your complaints, it's valid to point out that the Standard PHP library (SPL) bundled with PHP 5 comes with a class that allows you to traverse arrays, called "ArrayIterator." But in this case, I'm going to build a custom array iterator, so you can see more clearly the use of a protected constructor.

Having clarified that point, it's time to show the definition of the aforementioned custom array iterator, which is as follows:   

class DataIterator implements Iterator, Countable

{

    protected $_pointer = 0;

    protected $_data = array();

   

    protected function __construct(array $data)

    {

        if (empty($data))

        {

            throw new Exception('Input data must be a non-empty array.');

        }

        $this->_data = $data;

    }

   

    // implement 'count()' method required by Countable interface

    public function count()

    {

        return count($this->_data);

    }

   

    // implement 'rewind()' method required by Iterator interface

    public function rewind()

    {

        $this->_pointer = 0;

    }

   

    // implement 'current()' method required by Iterator interface

    public function current()

    {

        if (!$this->valid())

        {

            return FALSE;

        }

        return $this->_data[$this->_pointer];

    } 

   

    // implement 'valid()' method required by Iterator interface

    public function valid()

    {

        return $this->_pointer < $this->count();

    }

   

    // implement 'next()' method required by Iterator interface

    public function next()

    {

        ++$this->_pointer;

    }

   

    // implement 'key()' method required by Iterator interface

    public function key()

    {

        return $this->_pointer;

    }

}

As I said before, all that the above "DataIterator" class does is expose a short API that allows you to traverse arrays very simply. As the class has been defined as an implementer of the Iterator and Countable PHP 5 native interfaces, it must logically implement all of the inherited methods.

Due to its generic nature, the class is set up to be extended by other subclasses, instead of dealing directly with instances of itself. To prevent this instantiation in a very rudimentary fashion, the class declares its constructor protected, which recreates a basic scenario where a restrictive constructor can be used to avoid creating certain type of objects.

Naturally, if you closely analyze the definition of this iterator class, you'll realize  that there's a simpler way to prevent its instantiation. Declaring the iterator abstract will get the job done without having to deal with a restrictive constructor. Nonetheless, for the moment I'll keep the class's code untouched, so you can see from a basic example that a protected constructor may be pretty useful in certain situations.

Now that you've grasped the logic of the previous array iterator class, it's time to extend its functionality by creating a brand new subclass from it. For this example, the subclass will be used for traversing text files.

To learn more on how this subclass will be defined, read the section to come. It's only one click away.  



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