* the visit() methods must not only have different parameters * but different names too. */ public function visitSingleInputValue(SingleInputValue $inputValue); public function visitMultipleInputValue(MultipleInputValue $inputValue); } /** * A ConcreteVisitor. * Filters all the values provided casting them to integers. */ class IntFilter implements Visitor { } /** * Another ConcreteVisitor. * Sorts multiple values. */ class AscendingSort implements Visitor { } $userId = new SingleInputValue("42"); $categories = new MultipleInputValue(array('hated' => 16, 'ordinary' => 23, 'preferred' => 15)); $userId->acceptVisitor(new IntFilter()); printf("%s<br>", is_int($userId->get()) ? "OK" : "ERROR"); $categories->acceptVisitor(new AscendingSort()); $result = array_values($categories->get()); if ($result === array(15, 16, 23)) { echo "OK"; } else { echo "ERROR"; }
public function visitMultipleInputValue(MultipleInputValue $inputValue) { $values = $inputValue->get(); asort($values); $inputValue->set($values); }