Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple ways of calling parent method in php

Tags:

php

At first I was confused why both of the method calls in the constructor work, but now I think I understand. The extending classes inherit the parent's methods as if they were declared in the class itself, AND the methods exist in the parent, so both should work.

Now I'm wondering if there is a preferred way (i.e. best practice) of calling the method (via parent or this), and whether or not these are truly identical ways of executing the same code, or if there are any caveats when using one over the other.

Sorry, I'm probably over thinking this.

abstract class Animal {

    function get_species() {

        echo "test";

    }

}

class Dog extends Animal {

    function __construct(){

        $this->get_species();
        parent::get_species();

    }

}

$spike = new Dog;
like image 766
jerry Avatar asked Jun 28 '12 03:06

jerry


People also ask

How do you call a method from a parent class in PHP?

To call the constructor of the parent class from the constructor of the child class, you use the parent::__construct(arguments) syntax. The syntax for calling the parent constructor is the same as a regular method.


2 Answers

There are three scenarios (that I can think of) where you would call a method in a subclass where the method exists in the parent class:

  1. Method is not overwritten by subclass, only exists in parent.

This is the same as your example, and generally it's better to use $this -> get_species(); You are right that in this case the two are effectively the same, but the method has been inherited by the subclass, so there is no reason to differentiate. By using $this you stay consistent between inherited methods and locally declared methods.

  1. Method is overwritten by the subclass and has totally unique logic from the parent.

In this case, you would obviously want to use $this -> get_species(); because you don't want the parent's version of the method executed. Again, by consistently using $this, you don't need to worry about the distinction between this case and the first.

  1. Method extends parent class, adding on to what the parent method achieves.

In this case, you still want to use ``$this -> get_species();` when calling the method from other methods of the subclass. The one place you will call the parent method would be from the method that is overwriting the parent method. Example:

    abstract class Animal {

        function get_species() {

            echo "I am an animal.";
 
        }

     }

     class Dog extends Animal {

         function __construct(){

             $this->get_species();
         }

         function get_species(){

             parent::get_species();
             echo "More specifically, I am a dog.";
         }
    }

The only scenario I can imagine where you would need to call the parent method directly outside of the overriding method would be if they did two different things and you knew you needed the parent's version of the method, not the local. This shouldn't be the case, but if it did present itself, the clean way to approach this would be to create a new method with a name like get_parentSpecies() where all it does is call the parent method:

function get_parentSpecies(){

     parent::get_species();
}

Again, this keeps everything nice and consistent, allowing for changes/modifications to the local method rather than relying on the parent method.

like image 62
Anthony Avatar answered Oct 17 '22 14:10

Anthony


Unless I am misunderstanding the question, I would almost always use $this->get_species because the subclass (in this case dog) could overwrite that method since it does extend it. If the class dog doesn't redefine the method then both ways are functionally equivalent but if at some point in the future you decide you want the get_species method in dog should print "dog" then you would have to go back through all the code and change it.

When you use $this it is actually part of the object which you created and so will always be the most up-to-date as well (if the property being used has changed somehow in the lifetime of the object) whereas using the parent class is calling the static class method.

like image 7
hackartist Avatar answered Oct 17 '22 14:10

hackartist