NotionCommotion Posted August 21, 2016 Share Posted August 21, 2016 Given the following script, how should I eliminate duplicated script? class p { // ... } class c1 extends p { public function foo() { //Some common code to all foo's //Some specific code to c1::foo() which needs variables set above, and will set variables needed below. //Some common code to all foo's } } class c2 extends p { public function foo() { //Some common code to all foo's //Some specific code to c2::foo() which needs variables set above, and will set variables needed below. //Some common code to all foo's } } class c3 extends p { public function foo() { //Some common code to all foo's //Some specific code to c3::foo() which needs variables set above, and will set variables needed below. //Some common code to all foo's } } Quote Link to comment https://forums.phpfreaks.com/topic/301961-parent-child-ineritance/ Share on other sites More sharing options...
Jacques1 Posted August 21, 2016 Share Posted August 21, 2016 (edited) Possibly with the Template Method pattern. <?php abstract class P { public function foo() { echo 'P before<br>'; $this->inner(); echo 'P after<br>'; } abstract function inner(); } class C1 extends P { public function inner() { echo 'C1 inner<br>'; } } $c1 = new C1(); $c1->foo(); However, since you only post fantasy code, it's hard to tell. Edited August 21, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301961-parent-child-ineritance/#findComment-1536429 Share on other sites More sharing options...
NotionCommotion Posted August 22, 2016 Author Share Posted August 22, 2016 Possibly with the Template Method pattern. However, since you only post fantasy code, it's hard to tell. You know I love my fantasy code! I hope you know that I don't do it to make life easier for myself as it is far easier to slap down whatever I have previously written, and only do so in hopes that it better highlights what I am asking and makes it easier for others to either provide advice or for others to learn by the advice given. I've seen other posts with 500 lines of code, and it is difficult to know where to start, and it seems the objective is just to have someone write their code and not teach them to write for them self. Sounds like fishing! Just my two bits, for what it is worth... Okay, Template Method pattern is one option. Looks like we call the parent, and have the parent fill in the blanks using the child. I've been maybe wrongly dubious about calling the parent and having it call the child as it has eventually brought be down a dead end, but given my example, I see no issues. Are there other design patterns one might take? Why would one wish to take one pattern over another? I understand that this is a big question, and maybe there are documents already written on the subject, and if you know of any good ones, please direct me to them. If you feel like writing your own, I will surely welcome it as well! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/301961-parent-child-ineritance/#findComment-1536435 Share on other sites More sharing options...
Jacques1 Posted August 22, 2016 Share Posted August 22, 2016 The reason why I have a problem with abstract example code is that it's very hard to make an informed decision, and sometimes the examples are so bad that the whole discussion is useless for the actual problem. I'm not saying that you should dump your entire application in the forum, but a short description of the underlying project and an extract of the actual code are almost always better than some hypothetical class Foo calling a hypothetical method bar. Quote Link to comment https://forums.phpfreaks.com/topic/301961-parent-child-ineritance/#findComment-1536436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.