1

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;

1 Answer 1

1

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! =)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. But what happens if my object already has properties in it? Wouldn't reloading the variable reset all set properties? And could you explain a little more about passing by reference? I've read the PHP manual and know how it works, but I don't quite get how it can be applied in this situation.
Unless I'm making some grave mistake in understanding what you're asking, I don't believe there would be a problem. You're running the same class, so everything will be identical apart from the extension. Run a test case yourself, and come back if you have any issues! And by passing by reference, I was simply giving an alternative to explicitly resetting the variable. It's not really important either way for your purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.