How can I make only the parent method run in a class inheritance on PHP?

Alberto V.

I have some inquiries aboute this example, I make this simple code, just two classses, a parent and son class. When I execute the son must execute the parent and the parent method can execute himself BUT the program must not execute the son method.

When I run this program, execute both, parent and son. Exist any to prevent this and execute only the father's method?

class Father{

    private $element = 0; //Just to prevent recursivity

    public function add($a = null){

        if ($this->element == 0) {
            echo "<br>I'm the father"; //Execution of fhter
            $this->element = 1;

            $this->add('<br> I was an accidente'); //This instruccion call both methods, parent and soon
        }else{
            echo "<br>But not anymore"; 
        }
    }
}
class Son extends Father{
    public function add($a = null){

        parent::add();

        echo "<br>I'm the son";
        if ($a != null) {
            echo $a;
        }
    }
}

$son = new Son();
$son->add();

And I have these results

I'm the father
But not anymore
I'm the son
I was an accident
I'm the son

Like you see, when I execute the $this->add() method on parent, they execute both methods (add of father and son).

Is there any way to perform this code so that when executing $this->add() on the father, it does not execute both (the father and the son)?

In other words, I was expecting the next result

I'm the father
But not anymore
I'm the son
I was an accident

BY THE WAY: I cant modify the Father class. Thanks

Rasclatt

You just need to add the $element back into the Son class and use it like you did in the Father class:

class Son extends Father
{
    # Add this item back
    private $element = 1;

    public function add($a = null)
    {
        parent::add();
        # Check if it's set
        if($this->element == 1) {
            # Echo
            echo "<br>I'm the son";
            # Set to 0 so it doesn't recurse
            $this->element  =   0;
        }
        if ($a != null) {
            echo $a;
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make my class decorator only run on the outermost class in an inheritance chain?

How can i make a method available only from within the class

Inheritance: how to call method of parent class?

How can I make an interface instance method accept arguments of the same class only, really?

How can I make an interface instance method accept arguments of the same class only?

How can i make this php class static

How can i define a method in a class that it return only part of the class

How can I make Ubuntu to run only one application on startup

How can I make a .bat file run only in system tray?

How can I make the onMouseOver function run only once? | Html

PHP Class Method Inheritance

How can I run cleanup method only after tagged tests?

How can i grab some Parent Class Atribute values using Multiple Inheritance

How can I make a method to take only serializable objects?

how can I make this calculator using only one method?

How can I verify a method is run in a mocked class's callback?

How can I run a class method stored in a variable in Powershell

How can I make a method accept a class type variable in Java?

How can I make a class method that takes a function as a parameter?

How do I make a method of a class a closure in php?

How can I check class (not object) inheritance?

Inheritance , how can i make two classes share the same base class contents?

C++ How can I combine and add functionality to the same inherited method by a class with multiple inheritance?

How can I make an object in my parent class but bless it into my child class in Perl?

How can I enable nested class template only for parent class variadic template arguments?

How to make a method accessable to only one class?

How can I tell a function of parent class is called by classmethod or instance method from child class?

How can I decide to call an overrided method in child class from either parent or child class?

If I only know a class or ID of a parent element, how can I set a blur listener on a child textarea?