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 8951101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:35:05+00:00 2026-06-15T13:35:05+00:00

I can use at least two basic ways to access a protected class method

  • 0

I can use at least two basic ways to access a protected class method from a child class:

parent::myMethod();

$this->myMethod();

If I don’t need to override it in the child class, in which case I would have to do this:

function myMethod() {
   ...
   parent::myMethod();
   ...
}

which is the most recommended way to call it? I personally feel more comfortable using parent::myMethod() rather than $this->myMethod, because the first one immediately tells me this method is being inherited. But I’m not sure which way in terms of performance and best practices.

EDIT:

Check this, which is the real case of my question. It’s using CodeIgniter, but even though you’re not familiar with it, you will likely get it:

class Admin_Controller extends CI_Controller {

    protected function validate_form($validation) {
            $this->load->library('form_validation');
            // This will validate the form sent against the validation rules group specified in $validation
            if ($this->form_validation->run($validation) == false) {   
                    throw new Exception('There are errors in the form');
            }
    }

}

class Articles extends Admin_Controller {

    function save($id) {
            $this->validate_form(strtolower(get_class($this));
            // OR
            parent::validate_form(strtolower(get_class($this));
            // Other actions
            ....
    }

}

class Login extends Admin_Controller {

    function signIn() {
            $this->validate_form(strtolower(get_class($this));
            // OR
            parent::validate_form(strtolower(get_class($this));
            // Other actions
            ....
    }

}
  • 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-15T13:35:06+00:00Added an answer on June 15, 2026 at 1:35 pm

    They do two different things. The semantic of the parent keyword is such that it does a forwarding call to its parent class. The semantics of $this->method(), however, are such that it will only forward its call to the parent class in the event that the instance on which the method is called does not declare that method. It’s also important to note that a child may have a parent as well as a grand parent so that must be taken into careful consideration as well in the design of the prototype.

    If you don’t want the method to be overridden you should use the final keyword in the method prototype declaration.

    class MyClass {
        final protected function myMethod() {
        }
    }
    

    This ensures that any extending class will not be able to override the method. If an extending class attempts to override this method like the following code, you would get a fatal error of Fatal error: Cannot override final method MyClass::myMethod().

    class MyOtherClass extends MyClass {
        public function myMethod() {
        }
    }
    

    Also, this makes your code more readable and less prone to error since you are clearly stating to whoever reads the code that the method is final and cannot be overridden.

    Also, the underlying problem with your approach (if you want to use parent::myMethod() vs. $this->myMethod()) is that you aren’t taking into account what happens when the class is a grand child of its parent, for example…

    class Father {
        protected function myMethod() {
            return __METHOD__;
        }
    }
    
    class Child extends Father {
        public function myMethod() {
            return __METHOD__;
        }
    }
    
    class GrandChild extends Child {
        public function myOtherMethod() {
            echo parent::myMethod(), "\n"; // Child::myMethod
            echo $this->myMethod(), "\n";  // Child::myMethod
        }
    }
    
    $obj = new GrandChild;
    $obj->myOtherMethod();
    

    As you can see they will both give you the child method even if you meant to get at the father’s method. Just declare the method as final if you never intend to override it in the extending classes and you should always be fine when calling $this->myMethod() from object context.

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

Sidebar

Related Questions

I can use this maven plugin maven-jaxb-plugin to generate Java Classes from XSD file.
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
I'm looking to use a LEFT JOIN query to acquire some results from two
All my models contain at least two associations. When modeling this in ef4 I've
Let me rephrase my last question, what PHP library or framework can I use
I can use the following to get the contents of a folder with no
Users can use their Google, Facebook or Twitter account to login to a site
I can use .button() to create beautiful submit buttons, but the rest of the
I can use FireFox and FireBug, in a pane, I can open a .css
I can use the PRINT statement in a stored procedure to debug my code.

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.