Invoke inherited member functions using $this->my_func() or parent::my_func()?

I've been creating abstract classes and interfaces to force consistency within my Wordpress plugins, but I'm never sure if I should be invoking inherited functions by using parent::some_function() or using $this->some_function()? Since jumping back and forth between the two looks super messy/confusing.

For Example: Using getter/setters should I be:

$this->get_my_var(); // indicating nothing about its origins except through comments


parent::get_my_var(); // indicating you can find it in the parent


I only use parent:: in constructors, destructors and static methods. For everything else I trust people to know how object inheritance works. I would use $this->get_my_var();

They're not the same thing.

class A {
    protected function foo() {
        return "a";
class B extends A {
    protected function foo() {
        return parent::foo() . "b";
    public function bar() {
        return $this->foo();

$b = new B();
var_dump($b->bar()); // "ab"

If instead you had:

class B extends A {
    public function bar() {
        return parent::foo();

var_dump($b->bar()); // just "a"

The foo function of B adds something to the foo function of A. This is a common pattern.

Whether calling parent::foo in bar is good or not depends on your design choices, I personally think it's a little iffy. Calling parent::foo in foo is fine by the way.

Tags: PHP / Wordpress / Oop

Similar questions

Failed to invoke other hook from the init hook
Related to this question (Use wp init hook to call other hooks?) but not the same. Sometimes, I found that the hook will failed to run when I place inside the init hook, e.g. Not Work: Work: Of course some hooks/filters will just work okay inside the init hook, but definitely not all of them. So it seems to me that it is a bad practice to chain act...
how to invoke wordpress API from other existing PHP system
We have our own php system, now we want to have a blog function. Because wordpress are widely used, so we decided to use it. And now I want to invoke wordpress's function , for example wp_insert_user , how can I can do that ? Suppose my system is A and installed on /var/www/A, and wordpress is WP and installed on /var/www/WP. I can put them togethe...
Invoke a function before the action hook
I want to invoke a function before a special action. I want to validate the input of a custom metabox (or custom field) before the changes are saved. If the input value is invalid, then a stop save action with a message should be invoked. If the input fiels is valid, input value should be saved to DB. Please advise for me how to implement this prob...
Where and when does WordPress invoke routes
One of my URLs for which I'm wanting to create a Category Archive is coming up with a 404 despite having matching routes and, supposedly, matching templates. I'm going through the core WordPress code (groan) and haven't figured out where routes are invoked. It seems that the Categories are decided in query.php in the parse_query function however I ...
How to invoke a HTML custom button based on a HTML dropdownlist menu
I am a WordPress beginner and want a help I created a HTML dropdown list like this: and also I created a HTML button like this: I am trying to search about the code that I must use to : when a user select a value from dropdown list then the user click on my button I get this value to my custom page and preview the list of restaurants in it
why the wp_query invoke the wrong category post?
On the php file I am using the code: The above code not only invoked the category whose id is 11, it also invoked other category . Why?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.