I have a library that I use in several applications. It has a method that returns a URL:
class UrlBuilder {
public function url($config) {
$config = do_some_checks($config);
return make_url($config));
}
}
Often an application wants to alter the way the URL is built, and I would like to make that clear from the way the library is implemented. I came up with these solutions:
Mention it in the library docs, but otherwise just let the application override the method like any other:
class MyAppUrlBuilder extends UrlBuilder { public function url($config) { $config = do_some_checks($config); return make_url_differently($config); } }
Call a separate function that is just there to be overridden:
class UrlBuilder { public function url($config) { $config = do_some_checks($config); $config = $this->alterConfig($config); return make_url($config)); } protected function alterConfig($config) { return $config; } }
Provide a setter to set a customization callback:
class UrlBuilder { public function url($config) { $config = do_some_checks($config); if ($this->configModifier) $config = $this->configModifier($config); return make_url($config)); } public function setConfigModifier($configModifier) { $this->configModifier = $configModifier; } }
Is there a semantic difference and (if that is not a matter of taste) is any of them preferable?