Home arrow PHP arrow Page 4 - Defining an Abstract Class with Restrictive Constructors in PHP 5

Traversing the contents of a text file - PHP

In this second part of a four-part series, I rebuild the example application developed in the previous tutorial. As you'll recall, it used a protected constructor to prevent the instantiation of the base array iterator class. In this case, a better result will be achieved by declaring the iterator abstract; still, the example demonstrates a simple utilization of a restrictive constructor in a concrete situation.

TABLE OF CONTENTS:
  1. Defining an Abstract Class with Restrictive Constructors in PHP 5
  2. Review: using a protected constructor within an array iterator class
  3. Declaring the DataIterator class abstract
  4. Traversing the contents of a text file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 09, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the preceding segment, you just saw that the definition of the file iterator class derived from the base array iterator remained exactly the same, which means that in this particular case, putting it to work wouldnít be radically different from other code samples shown previously. But is this really true? Well, I have to say yes, and the following code fragment demonstrates that:

// use FileIterator class

try

{

    // create instance of FileIterator

    $fit = new FileIterator('sample_file.txt');

    // reset iterator pointer

    $fit->rewind();

    // display current file line

    echo $fit->current();

    // move iterator pointer forward

    $fit->next();

    // display current file line

    echo $fit->current();

    // move iterator pointer forward

    $fit->next();

    // display current file line

    echo $fit->current();

    // reset iterator pointer

    $fit->rewind();

    // display current file line

    echo $fit->current();

}

 

 

 

// catch exceptions

catch (Exception $e)

{

    echo $e->getMessage();

    exit();  

}

Nothing really unexpected, right? As you can see, iterating over the contents of a text file is a process that doesnít differ at all from the example that you saw in the introductory installment. In addition, itís valid to stress that even though turning the base iterator into an abstract class permitted us to get rid of a protected constructor, there are other scenarios where a restrictive constructor can be of great help to implement certain patterns, or even assure that a class will be used out of the object's context.

However, some of these topics will be discussed in more detail in upcoming parts of the series.  

Final thoughts

In this second episode of the series, I rebuilt the example application developed in the previous tutorial, which used a protected constructor to prevent the instantiation of the base array iterator class. In this case, itís valid to stress that a better result was achieved by declaring the iterator in question abstract, but hopefully the example was useful enough to demonstrate a simple utilization of a restrictive constructor in a concrete situation.

As I said in the introduction of this series, protected and private constructors can be used in a great diversity of scenarios and contexts. Therefore, in the subsequent tutorial Iím going to explain how to work with a private constructor to implement a Singleton database access class.

Donít miss the next part!



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