Jump to content

Plugin calls to functions or Class Methods? Which is better?


Xeoncross

Recommended Posts

 

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?

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

 

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...

 

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.