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

Building a basic file iterator - 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.

  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



In the previous section, I demonstrated how to build an array iterator class that, thanks to the implementation of a protected constructor, can't be directly instantiated. Given that restriction, the next step that I'm going to take will consist of deriving a subclass from the iterator. The subclass will declare a pretty refined constructor method for traversing simple text files.

Please take a close look at the definition of this new file iterator class, which is as follows: 

  class FileIterator extends DataIterator


    private $_file = 'data.txt';


    // override parent constructor

      public function __construct($file = '')


        if ($file !== '')


            if (!file_exists($file))


                throw new Exception('Target file must be an existing file.');


            $this->_file = $file;    


        $data = file($this->_file);




As depicted above, the logic that drives the "FileIterator" class is extremely easy to follow. It simply overrides its parent's constructor to take as an input argument the text file that must be iterated over. The remaining methods are simply inherited from the base "DataIterator," so I'm not going to get back to them again, since they were already discussed in the previous section.

So far, everything looks pretty good, right? At this point, there's a class that allows you to traverse text files in a painless fashion, thanks to the inherited functionality of a base class which implements a protected constructor. At the risk of being repetitive, I'd like to stress that a better result could be produced by declaring the corresponding parent abstract, but for now the parent iterator will live as a concrete class.

Having explained how the file iterator does its thing, it's time to give it a try so you can see it in action. Therefore, in the last section of this tutorial I'm going to set up an example for you, which will demonstrate how to iterate easily over the contents of a sample text file.

To see how this particular example will be developed, click on the link below and read the following segment. 

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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