PHP OOP – Extending classes

I’m quite new to OOP and there are a few things I’d like to ask.

I’m writing a plugin for WordPress using OOP and I want to find out how I can allow other plugin developers to extend and redefine certain functions.

Read More

I have the main code wrapped in a class as follows and the class is instantiated at the bottom of the file:

class MyPlugin {

    // .. methods here ..

    public function myMethod(){
        // do something
    }

}

$myplugin = new MyPlugin;

However, I also want another plugin to be able to rewrite myMethod() by extending the class.

class OtherPlugin extends MyPlugin {
    public function myMethod(){
        // do something else
    }
}

But in order for $myplugin->myMethod() to ‘do something else’, should I do this to override the $myplugin object:

$myplugin = new OtherPlugin;

Related posts

Leave a Reply

1 comment