mattnoble Posted January 25, 2007 Share Posted January 25, 2007 I was just curious if anyone knew the general theory behind how to create PHP applications with addon support. How would you implement addons that people create and how exactly are they created. Are the addons just classes and functions that are added into the code somehow. I don't even know if I'm wording this right haha. What comes to mind mainly is WordPress, and it's huge addon repository and I was just curious if anyone knew how this was done, not specifically for WP, but in general. Thanks a lot! :) Quote Link to comment Share on other sites More sharing options...
Jenk Posted January 25, 2007 Share Posted January 25, 2007 Implement the Observer Pattern into the 'core' of your application (usually invoked by the front controller and stored in a registry.) Use strong interfaces, and provide plenty documentation.Voila.. one plugin system.EDIT: oh, and to implement the observer pattern is more than to just create a notify object, you'll actually need to have your application use it :) Quote Link to comment Share on other sites More sharing options...
mattnoble Posted January 25, 2007 Author Share Posted January 25, 2007 That was fast haha. Thanks! Now, off to read some things :) Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 25, 2007 Share Posted January 25, 2007 The most straightforward way to implement a plugin system is to use php5 interfaces http://us3.php.net/interfaces.Consider the following:[code]<?phpinterface Foo{ public function getPluginName(); public function mutateSomeObject(&$obj);}?>[/code]By using in an interface you are forcing plugins to implement certain methods, in this case getPluginName and mutateSomeObject.A 3rd party plugin might look something like this:[code]<?phpclass SomePlugin implements Foo{ public function getPluginName() { return 'MyPlugin'; } public function mutateSomeObject(&$obj) { $obj->setProperty('foo'); }}?>[/code]When you want to act on your plugin you might do something like this:[code]<?php//..$plugin =& PluginManager::getInstalledPlugin('SomePlugin');echo 'Loading plugin ' . $plugin->getPluginName() . '...';$plugin->mutateSomeObject($someObject);?>[/code]Best,Patrick Quote Link to comment Share on other sites More sharing options...
mattnoble Posted January 30, 2007 Author Share Posted January 30, 2007 Cool, thank a lot Utexas! Hopefully I'll be able to get something going in the future. Appreciate it guys :) Quote Link to comment Share on other sites More sharing options...
Jenk Posted January 31, 2007 Share Posted January 31, 2007 Passing objects by reference raises E_STRICT errors, fyi. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.