2

Call a method in the parent of the parent (the gran pa)

 2 years ago
source link: https://www.codesd.com/item/call-a-method-in-the-parent-of-the-parent-the-gran-pa.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Call a method in the parent of the parent (the gran pa)

advertisements

I am making a tournament generator plugin in laravel: https://github.com/xoco70/kendo-tournaments

So basically,

- a championship can be made with team or with competitors
- a championship can have preliminary round
- a championship can be Direct Elimination or PlayOff

I initially did it in a single class TreeGen, but there were a lot of :

if ($championship->isDirectElimination) or if ($championship->hasPreliminary), etc... This can make a lot of case, and complexity increased too much. So, I refactored like this

SuperClass: TreeGen

This class has all common methods

Then 2 children class:

DirectEliminationTreeGen extends TreeGen

PlayOffTreeGen extends TreeGen

Then for each class, I created to children:

DirectEliminationTeamTreeGen extends TreeGen

DirectEliminationCompetitorTreeGen extends TreeGen

PlayOffTreeTeamGen extends TreeGen

PlayOffCompetitorTreeGen extends TreeGen

In each class, I only put method that vary.

This approach has for me several advantages :

  • It break a single big class into smaller ones
  • It removes conditions that make my code more and more difficult to maintain.

Now, my problem is in the case of DirectElimination with Team.

First, Before generating, I have a method chooseGenerationStrategy that determine the case:

public function chooseGenerationStrategy()
{
    $generation = new TreeGen($this, null);
    switch (true) {
        case $this->isDirectEliminationCompetitor():
            $generation = new DirectEliminationCompetitorTreeGen($this, null);
            break;
        case $this->isDirectEliminationTeam():
            $generation = new DirectEliminationTeamTreeGen($this, null);
            break;
        case $this->isPlayoffCompetitor():
            $generation = new PlayOffCompetitorTreeGen($this, null);
            break;
        case $this->isPlayoffTeam():
            $generation = new PlayOffTeamTreeGen($this, null);
            break;
    }
    return $generation;
}

Then I execute $generation->run()

It appears that in the execution, I have in TreeGen:

    $byeGroup = $this->getByeGroup($this->championship, $fighters);

so in this case, $this is DirectEliminationTeamTreeGen type, but getByeGroup in PlayOffTreeGenand DirectEliminationTreeGen

So my initial though was to create a getByeGroup inside the DirectEliminationTeamTreeGen and call the parent, but it was a bad work around, and also, I realize that method is defined in the parent of DirectEliminationTeamTreeGen class but in the child of the calling class `TreeGen``

I understand the issue, as there is no getByeGroup() method inside DirectEliminationTeamTreeGen class, but don't know how to fix it.

Any Idea?


You can call it this way.

class A{
    public function parentMethod() {
    }
}

class B extends A{
}

class C extends B{
    public function anotherMethod() {
        $this->parentMethod();
    }
}

If you want the method parentMethod() to be visible inside child class, the method has to be public (accessible from outside of the class) or protected (accessible only from inside - parent and child classes). private methods are only visible in the class in which they are defined.

More info here: PHP visibility.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK