<?php

class Item {
 private $data;
 public function __construct ($value) {$this->data = $value;}
 public function __toString() {return strval($this->data);}
 public function isValid() {return $this->data != 666;}
}

function foreach_with_fitler($array, $iterator, $body, $fitler) {
  @array_walk(array_filter($array, $fitler), function($item, $key)use($iterator, $body){
    $$iterator = $key;
    eval($body);
  });
}

$items = array(new Item(13), new Item(265), new Item(666), new Item(1488));

define('FITLER', 'FITLER');

function FITLER($value) {return $value->isValid();}

foreach_with_fitler($items, '$item', 'echo $item . PHP_EOL;', FITLER);
