Just to put what ServerStorm was hinting towards on the table and re-introducing some flexibility and separation of concerns. I've severely cut down the class for ease of demonstration:
PHP Code:
interface DogRenderer {
public function render($name, $age);
}
class Dog {
protected $name;
protected $age;
public function dispatch(DogRenderer $renderer) {
$renderer->render($this->name, $this->age);
}
}
class MyDogRenderer implements DogRenderer {
protected $html;
public function render($name, $age) {
$this->html = 'Name: ' . $name . ' Age: ' . $age;
}
public function output() {
return $this->html;
}
}
$dog = new Dog();
$renderer = new MyDogRenderer();
$dog->dispatch($renderer);
echo $renderer->output();
Which should remove some of the valid criticism noted above.
However, my issue still stands, adding a single field e.g. "weight" requires:
-Adding the protected property
-Extending the interface (we can't modify DogRenderer as we don't know where it's currently used)
-Either adding a new dispatch method for the new interface to Dog
--Or adding some logic to the dispatch function to check which interface it's been passed (of course, if php supported method overloading this wouldn't be an issue)
Wheras with a getter or public property, you add the property and change the HTML. The former method is a maintainability nightmare, in my opinion.
Bookmarks