Embrace Inheritance
In the last week I've been working on writing a tutorial for our MvcTools component. For this tutorial I've also written a very simple "HelloMvc" application , not only to have something to write the tutorial about, but also to test how the component would do in a more real life situation, outside of the unit testing framework.
I realized during the development of this sample application, is that while writing software, you can never really implement things so that it does exactly what everybody wants and expects. During the development of our MvcTools component we're run into a few of those occasions. One of those is the naming conventions our default controller uses for action's methods. In our first implementation we would always require the method naming of "doActionName()".
Some of our users might want to do something else, but would now have to rewrite the whole class. That is of course unnecessary, as Object Oriented programming excels at reuse of code. However, in order for inheritance to work, all the functionality need to be separated as much as possible into their own methods.
In the re-factored version we now have split out the method name algorithm to its own method, createActionMethodName(). This allows for easier customization of code by inheriting from this class and just overriding this method. It also makes testing easier, as now it is possible to test the name generating algorithm. To illustrate this, we actually found an issue while writing the tests for this new method.
Comments
Hi Derick! I would have some notes on the second solution:
-
I am not sure if it is a good practice to automatically expand the request into variables. For first view maybe it can confuse somebody reading an action code, but of course it is not a big issue while the programmer should know that this is how this framework works. But I think it is much useful if these variables are used for the following purpose: if you set a variables it will be automatically accessible in the view layer.
-
I usually don't like to modify the request, just validate it, and if it is not valid pass back to the user. So I am not sure if it is neat that preg_replace in creating the action name. And maybe it would be logical to do it the dev mode section.
-
While the class's own methods are all protected maybe it would be sexier to define a __call method which throw the not found exception. And it would be faster for the normal flow while not the bad case is the general.
Life Line
Back Lit Great Crested Grebe
Just as I was about to leave the London Wetland Centre, this Grebe was sitting on a lake, perfectly lit from behind.
#photography #Birds #BirdPhotography #BirdsOfFediverse #NaturePhotography #wildlife #wwt #london
I hiked 13.4km in 6h21m29s
Long-Tailed Tit, Being Cute
While getting cold in the Purfleet Hide at RSPB Rainham Marshes, a flock of these little birds went for the tree next to us. Chattering away a bunch.
This one stayed long enough for a snap.
#photography #Birds #BirdPhotography #BirdsOfFediverse #NaturePhotography #wildlife #RSPB #RainhamMarshes
Looking forward to seeing this get merged once the niggles are ironed out.
https://web-php-pr-2105.preview.thephp.foundation/
Survey opening hours
Specify whether pedestrian crossings have markings
Specify whether pedestrian crossings have islands
Survey if places still exist
Specify cycleways width
Specify bollard types
Specify whether traffic signals have sound signals
Specify width of opening
Specify whether traffic signals have tactile indications that it's safe to cross
Survey if places still exist
Specify whether crosswalks have tactile paving
Specify whether kerbs have tactile paving
Determine the heights of kerbs
Determine roads lane count
Add whether public transport stops are lit
Specify whether public transport stops have tactile paving
Specify type of recycling amenities
Specify step counts
Survey tactile paving on steps
Specify whether steps have a ramp
Specify which way leads up for steps



Shortlink
This article has a short URL available: https://drck.me/ei-6i2