I have a front controller that is instantiated as follows:
$request = new FrontController();
$request->route();
The route() method then dynamically calls the appropriate controller as needed based on the request.
Now (I think) I want all of my controllers to extend the FrontController so that they can all have access to a shared set of methods and properties, but I don’t want them to inherit the route() method as that could potentially lead to an infinite loop. If the route() method is marked as private, however, then I can’t instantiate the object as demonstrated above.
The only solution I’ve found is to call self::route() from the FrontController’s constructor, and then to define a blank constructor in each child controller. That just feels sloppy.
Is there a way to exclude certain methods from inheritance without marking them as private? Or should I be looking at the problem from another angle?
You can prepend the
finalmodifier to a method to prevent it from being overridden by subclassing.See the following PHP manual entry for more examples and a detailed explanation:
http://php.net/manual/en/language.oop5.final.php