Xeoncross Posted June 11, 2008 Share Posted June 11, 2008 I have been struggling with this for some time now. I really want to use classes to structure and organize my plugin code. Take a look at a code clip from a system called chyrp.net. They use class methods as there plugin system. But I have found that using functions in plugin calls (like the way drupal.org does it) is more flexible and is faster for PHP to handle. ////////Functions plugins are collections of functions ## GOOD ## simple names - any plugin can add a trigger to any other function $plugin->add(function_name); Don't have to use the CPU resources of classes ## BAD ## not as structured as classes? ////// Methods plugins are classes ## GOOD ## You can map the page request to a method of the class (like zend) site.com/posts/recent -> posts::recent(); ## BAD ## you would have to use class names for functions plugin::my_function($text); You would have to add the method of the function to the url: site.com/class/method instead of site.com/plugincall/ I don't know if I am making this clear, but I want to know if I am missing anything in going with a file full of plain functions that are called individually instead of using a single class that has different methods called when needs. Can anyone add to this? Quote Link to comment Share on other sites More sharing options...
keeB Posted June 11, 2008 Share Posted June 11, 2008 <?php interface PluginInterface { function execute(); //each plugin must have a execute() method. } class PluginLoader() { public static function loadPlugIn($plugin) { // return the plugin object } public function register($plugin) { // registers a plugin in registry } } class MyPluginImpl implements PluginInterface { function execute() { print "lol"; // mylolplugin } } ?> Seems pretty straight forward to me using objects. Quote Link to comment Share on other sites More sharing options...
Xeoncross Posted June 11, 2008 Author Share Posted June 11, 2008 Yes, but what if you have a plugin that has 7 functions from creating sidbar HTML to processing requests. You can just use the execute() for everything. <?php //My Plugin function plugin_install() {} function plugin_uninstall() {} function plugin_donothing() {} function plugin_recent_posts() {} function plugin_count_to_twenty() {} ?> I am looking for something that you can't do easily (or the right way) with just functions that you can do with methods. Something that would make the points in the first post go away... Quote Link to comment Share on other sites More sharing options...
keeB Posted June 11, 2008 Share Posted June 11, 2008 So you want to implement a plugin system with callbacks. Doesn't really change the idea very much. Quote Link to comment Share on other sites More sharing options...
Xeoncross Posted June 13, 2008 Author Share Posted June 13, 2008 Actually, I already implemented one based off of functions - because I never found a compelling reason to use classes. That is why I am posting here. I would like to glen from others the possible things I might be missing. I don't need to know how to implement such a system - I just want to know the design errors that come from both choices. Quote Link to comment Share on other sites More sharing options...
keeB Posted June 13, 2008 Share Posted June 13, 2008 Having not written a plugin system recently which implements callbacks, I cannot accurately engage in what would be considered a fruitful conversation. What I can say is this. If you've found a method which plays to your style, then by all means it's the proper method for you. Is there anything you see as a downside to the route you've chosen to go? I'm guessing so, since you came to the board asking about alternatives. So, what's up? What's working? What isn't? Quote Link to comment Share on other sites More sharing options...
Xeoncross Posted June 13, 2008 Author Share Posted June 13, 2008 Well, like I listed above the only thing that I have found wrong with using functions instead of classes - is that not many other systems do it that way (therefore I must be missing something). However, I have found that with functions: You Avoid Wasted System Resources You don't have to use myclassname:myfunctionname() To call a plugin you can do site.com/function/ instead of site.com/class/method 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.