Multiple ways of calling parent method in PHP

0 votes

I wasn't sure why both methods calls in the function Object() { [native code] } worked at first, but I think I now understand. Since the methods are passed down from the parent class and are also present in the extending classes, both should function.

Now I'm wondering whether there is a preferred technique (i.e. best practice) of calling the method (through a parent or this), whether these are actually identical ways of running the same code, or if there are any restrictions when using one over the other.

abstract class Animal {

    function get_species() {

        echo "test";

    }

}

class Dog extends Animal {

    function __construct(){

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

    }

}

$spike = new Dog;

Can someone please help me with this?

Jul 23, 2022 in PHP by Kithuzzz
• 38,000 points
2,660 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

There are three circumstances (that I can think of) in which you might call a method in a subclass when the parent class's method exits:

  1. The method only exists in the parent and is not overwritten by subclasses. You are correct that in this instance the two are practically the same, but the function has been inherited by the subclass, so there is no reason to differentiate. This is the same as your example, and generally speaking, it's best to use $this -> get species(). You maintain consistency between locally defined methods and inherited methods by using $this.
  2. The subclass replaces the method, which uses an entirely different logic from the parent. Given that you don't want the parent's version of the procedure to be used, you should use $this -> get species() in this situation. Again, you can avoid worrying about the difference between this example and the first by continuously using $this.
  3. Adding to what the parent method accomplishes, the method extends the parent class. When invoking the method from other methods of the subclass, you should still use "$this -> get species();" in this scenario. Only from the method that is replacing the parent method will you make a call to 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.";
     }
}

I hope this helps you.

answered Jul 24, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,656 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
3,192 views
0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7, 2020 in PHP by Niroj
• 82,840 points
11,471 views
0 votes
1 answer

What is the use of the @ symbol in PHP?

Hello @kartik, The @ symbol is the error control operator ("silence" or "shut-up" ...READ MORE

answered Apr 9, 2020 in PHP by Niroj
• 82,840 points
12,590 views
0 votes
1 answer

How PHP Replace last occurrence of a String in a String?

Hello @kartik, You can use this function: function str_lreplace($search, ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,840 points
3,112 views
0 votes
1 answer

How to resolve the problem of losing a session after a redirect in PHP?

Hello @kartik, Carry out these usual checks: Make sure session_start(); is ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,840 points
35,796 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
1,856 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
11,642 views
0 votes
1 answer

Failure uploading Image on AmazonS3 with PHP SDK

Try this, I took it out from ...READ MORE

answered May 4, 2018 in AWS by Cloud gunner
• 4,670 points
4,840 views
0 votes
1 answer

Trying to call AWS API via PHP

Try using AWS SDK for PHP, Link ...READ MORE

answered Jun 6, 2018 in AWS by Cloud gunner
• 4,670 points
1,911 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP