A very common problem is submitting each object of an array to a set of tests: if a given object fails a given test, the system marks the object as “failed” without running the remaining tests.
The classical solution is writing a class for the test, with a method for each test and another method that calls them like
if ($this->method_test_3($i) == false) {
print "method_test_3 failed\n";
}
if ($this->method_test_4($i) == false) {
print "method_test_4 failed\n";
}
but this solution is not elegant and, adding new filters, needs new blocks of code. Using reflection I can get the list of methods to call (using a little trick) without having to list them with repetitive blocks of code.
I’ve written a PHP script to show this idea (the array is made of strings instead of generic objects): you can download it here.
Last update: 2008-06-19
0 Responses to “Filter chains using reflection”
Leave a Reply