Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8162631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:46:40+00:00 2026-06-06T18:46:40+00:00

At first I was confused why both of the method calls in the constructor

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:46:43+00:00Added an answer on June 6, 2026 at 6:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing my first Rails app and I'm confused by a method call that
i'm writing my first MATLAB O-O application and i'm confused about the implementation of
I am building my first Symfony2 project and I may be confused on the
I'm confused. I'm trying to loop though 2 files looking at the first token
I am working on my first segue, and I am a bit confused. I
Switching to GCC for the first time, and I'm getting a bit confused by
There are two algorithms presented in the web. In both the algorithms, the first
I am confused by the first 2 params, module and command-line. I find unless
I have two dropdowns in html. Both dropdowns are getting data mysql first dropdown
First of all, sorry for the confused title. What I want to do is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.