mushroom
mushroom

Reputation: 1945

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.

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;

Upvotes: 1

Views: 165

Answers (1)

Nathanael
Nathanael

Reputation: 7183

Yes, you would want to reload that variable with the new, modified class... unless you pass by reference. Check it out... it's a neat little trick.

Happy coding! =)

Upvotes: 1

Related Questions