Jump to content

Modifying a method common to multiple classes


NotionCommotion

Recommended Posts

I have three classes which each have their own method "pm()".  Each of them call mainMethod(), and pass $a and $b.  All of them also need to pass a value for x and y except for one that also needs to pass a value for z, and as such, I was planning on passing an array for these "extra" values.

 

mainMethod is the same for all of them except one small section.  It is possible that mainMethod() will be called by more than once in a given class and will require different functionallity for this "small section".  Originally, I planned on having the pm() methods pass mainMethod() an anonymous function, but have since learned the error of my ways.

 

I recognize I am not providing "all the information", but am hoping that I have provided enough to get advice whether I am doing this the right way.

 

Thanks

<?php
class class1 extends mainClass
{
    public function pm()
    {
        //some script
        $extra=['x'=>1,'y'=>2];
        $this->mainMethod($a,$b,'pm_callback',$extra);
    }
    protected function pm_callback($extra,$n,$m)
    {
        //some script that uses $this->model, $extra, $n, and $m
    }
}

class class2 extends mainClass
{
    public function pm()
    {
        //some script
        $extra=['x'=>1,'y'=>2];
        $this->mainMethod($a,$b,'pm_callback',$extra);
    }
    protected function pm_callback($extra,$n,$m)
    {
        //some script that uses $this->model, $extra, $n, and $m
    }
}

class class3 extends mainClass
{
    public function pm()
    {
        //some script
        $extra=['x'=>1,'y'=>2,'z'=>3];
        $this->mainMethod($a,$b,'pm_callback',$extra);
    }
    protected function pm_callback($extra,$n,$m)
    {
        //some script that uses $this->model, $extra, $n, and $m
    }
}

class mainClass
{
    protected function mainMethod($a,$b,$callback,$extra)
    {
        $model=new model();
        //some script
        $this->$callback($extra,$n,$m);
        //some script
    }
}
?>
Edited by NotionCommotion
Link to comment
Share on other sites

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.