Archive for the ‘ PHP 5 ’ Category

Php 5 extend, array and __construct

// Declare a new class that extends xavier into regex
class zabyer extends xavier {
    // Declare a property to hold the filter criteria
    private $criteria;

    // Now make our constructor
    public function __construct(Iterator $i, $regex) {
        // Call the parents constructor, and store the regex
        $this->criteria = $regex;

    // Define accept
    public function accept() {
        // Just see if the regex matches the current item:
        return preg_match($this->criteria, parent::current());

// Now to test this, first define an array of values
$arr = array('macanhan', 'tibongco', 'gusa', 'gaisan', 'sm', 'cogon', 'agusan');
// Now let's create a chained Iterator.  Make a xavier that wraps
// around an ArrayIterator, and only allows items with a 'y' in them.
$it = new zabyer(new ArrayIterator($arr), '/y/i');

// Now loop and echo these all out, should include tibongco, sm, agusan.
foreach ($it as $entry) { echo "{$entry}
\n"; }