Jump to content

Recommended Posts

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
    }
}
Link to comment
https://forums.phpfreaks.com/topic/301961-parent-child-ineritance/
Share on other sites

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 by Jacques1

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.