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
What new fresh hell is this?
"Please click here and tick the box if you DO NOT want to be opted in."
And when you click on the non-visible link:
"[ ] I DO NOT want to be opted in."
@jamesholden Have you ever seen the Expanse? One of the main characters shares your name!
I walked 4.4km in 51m58s
I walked 8.2km in 1h40m07s
I walked 2.4km in 38m25s
@Edent With your ActivityPub implementation, have you figured out how to allow quote posts for your bot posts yet?
📷 Brown Cap in the Grass
🚩 Herikhuizerweg, Rheden, Nederland
I walked 0.9km in 11m17s
I walked 2.8km in 25m32s
I walked 4.6km in 1h8m02s
📷 Stalkers Lane
🚩 Graywood Lane, Wealden, United Kingdom
I hiked 23.0km in 4h10m15s
I walked 3.9km in 39m07s
After my PHP 8.5 in Leeds last night, it's now time to head to Rotterdam to give the same talk there tonight!
It's cold out here, but atleast the snow is now gone.
I walked 1.7km in 13m18s
📷 Avenue Gardens
🚩 Princess Road, London Borough of Brent, United Kingdom
📷 Leafy Entrance
🚩 East Heath Road, London Borough of Camden, United Kingdom
📷 From Green to Yellow.
🚩 St John's Wood Road, City of Westminster, United Kingdom
I walked 9.1km in 1h56m24s







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