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.
blog comments powered by Disqus |
|
|
|
|
|
|
|