Page 1 of 1

writing a code that accepts add-ons

Posted: Fri Oct 10, 2008 6:19 pm
by sam4free
Hi ..
Is there a theory or a main idea about this point
I mean make the code able to have new features from files added dynamically without changing anything in the original code.

I have googled a lot and found nothing
Tried to understand wordpress's way to achieve this goal but the source is too big to separate the desired code and understated it.

Re: writing a code that accepts add-ons

Posted: Sat Oct 11, 2008 12:00 am
by kpowell
In my experience, there are a couple ways to do this.

Using classes, you can simply extend the class with another class. Doing this, you are able to use the properties and methods from the parent, add to them, and even overwrite them (given that the permissions are not set otherwise). An example would look something like this:

parent.php

Code: Select all

<?php
class Parent {
    var $default = "I'm a default property.";
     // This is your construct.  in PHP 5, you can use __contruct instead of the class name.
    function Parent()
    {
    }
 
    function do_this()
    {
        echo 'I did it!';
    }
}
Then child.php

Code: Select all

<?php
class Child extends Parent {
    // again the construct
    function Child()
    {
        // if you don't specify the parent, you can't access the parent properties and methods
        parent::Parent();
    }
}
You can also add hooks into your code. I haven't actually written anything that does this, but I've used plenty of this kind of thing. Basically, you are accessing a class and checking whether something is set that corresponds to a particular part of your code. I'll give a rough example (this is a function in a class. $this->hook refers to another class):

Code: Select all

<?php
function do_stuff_more()
{
    if($this->hook) return $this->hook->do_more(/* some variables */);
}
Then $this->hook->do_more would find information about different functions to use either from a database or a configuration file that pertain to that hook. It allows you to manipulate the variables that you pass to the function outside of that function. Hope that makes sense. I'm sure someone with a bit more skill than I have can give better examples.

Re: writing a code that accepts add-ons

Posted: Sat Oct 11, 2008 4:46 am
by sam4free
Thank you kpowell
the two ideas you talked about are good - one of them have crossed my mind before -

the idea i'm looking for is something more like wordpress
I mean there is a folder where we put the -compatible- addon files
for example if we have a normal CMS, one addon file may add a poll to the sidebar of the main page. that addon must NOT affect nor get affected by other addons.

this is an example of a basic idea in my mind
we have an array of addon classes, we pass each the html code for the part of the page we are generating to let the addon add things to the code and return the result ot us

Code: Select all

<?
function get_sidebarcode(){
    //here goes the code that forms the sidebar
    $sidebar = -SOME SIDEBAR CODE-;
 
    //we have to call all addons that can add -or edit- things to the sidebar
    foreach($addons as $addon){
        $sidebar = $addon->get_sidebarcode($sidebar);
    }
 
    return $sidebar;
}
?>
is there some kind of engine to do that? i mean like template engines when someone wants to add templates to his/her work

Re: writing a code that accepts add-ons

Posted: Sat Oct 11, 2008 5:35 am
by onion2k
In PHP Image (see signature) I borrowed (and simplified) the plug-in system from Swift Mailer. Essentially it just a predefined API with an array of objects.. at certain stages the main object loops through the array of plug-ins calling the method that they all have (defined in an interface).

Re: writing a code that accepts add-ons

Posted: Sat Oct 11, 2008 2:35 pm
by sam4free
thanks onion2k -nice work on the library by the way-
i got some very good ideas from your code

still the problem not solved
make the code able to have new features from files added dynamically without changing anything in the original code
Wordpress is the best example about what i have in my mind

thank you again

Re: writing a code that accepts add-ons

Posted: Sat Oct 11, 2008 6:49 pm
by josh
This is just good code design, its not necessarily 1 technique there's 100s and 100s of patterns and the more you patterns you are familiar with the better decisions you will make at each step. The answer to your question could be anything from a simply using the include() function and overriding a bunch of methods higher up in your object hierarchy to a dependency injection framework, or many other techniques. If all of your plugins are going to follow a common interface you could implement your plugins as strategy patterns and "inject" them into a plugin controller like onion said that keeps a map of plugins and calls a fixed method on all registered plugins (this is basically chain of command pattern). If you don't want to hard code to a specific interface you could use the observable pattern to have your plugins register ad-hoc "events" that map to certain methods.

Another great pattern for accomplishing this seems to be chain of command. Each pattern has its own pros and cons and without knowing specifics of what kinds of things your plugins need to be able to do there's no way to settle on any 1 "best" pattern. Perhpaps it would be worth investing some time in understanding wordpress' code. You could also check out one of the many open source CMS' implemented in PHP and see how they do it, typically its with one of the design patterns me or the previous posters have mentioned.

Keep in mind design patterns are just abstract suggestions that help you conform to tried and true practices, without fully understanding the tools you have available you can never be sure you've picked the right tool. Most importantly of all I believe whatever tool you pick you should be aware of the other tools ( patterns ) and how they work, so you can write your code in such a way it would be easy to switch to another pattern later

Re: writing a code that accepts add-ons

Posted: Sat Oct 11, 2008 7:25 pm
by sam4free
jshpro2 I am afraid you are right
you convinced me that there is no "best" way to do this thing

i tired to understand wordpress code countless times but always ended lost in the huge amount of code :banghead:
i also was hoping to find some article that talks about this problem

well ... it seems that trying and learning form my mistakes is the only choice left to do

thank you, jshpro2

Re: writing a code that accepts add-ons

Posted: Sun Oct 12, 2008 11:34 am
by josh
Why don't you use an existing framework?

I searched for "writing event driven software php" and got this which looks cool
http://www.xisc.com/

Re: writing a code that accepts add-ons

Posted: Sun Oct 12, 2008 11:45 am
by Eran
By the way, I wouldn't take design inspirations from wordpress, that code is horrible...

Take a close look on how Onion implemented a plugin system for image manipulation in his library, it works really well.

Re: writing a code that accepts add-ons

Posted: Mon Oct 13, 2008 6:28 am
by sam4free
jshpro2 i the library you are talking about is for using AJAX and doesn't have anything about plugins 'It's very good library though'
using ajax libraries 'like PHPLiveX' makes creating a plugin system easier .. still I have no clear idea of how to achieve a good plugin system.

pytrin unfortunately you are correct ... but wordpress is the best example of what i have in mind
'Imagine how cool it will be if wordpress' code was clear enough to get all the good ideas from it ...'
onion's plugin system is very good .. still it needs the main code to be aware of what the plugins do, and call "plugin-specific" functions.

Thank you
I don't know if i clarified my idea enough, but English is not my native language..

Re: writing a code that accepts add-ons

Posted: Mon Oct 13, 2008 9:01 am
by josh
The library says its an event driven architecture, which is a technique that can be used to create a pluggable application. This is called inversion of control which is the essence of modular coding.

Re: writing a code that accepts add-ons

Posted: Mon Oct 13, 2008 10:33 am
by sam4free
i will study this library and hope it will be useful
thank you

Re: writing a code that accepts add-ons

Posted: Mon Oct 13, 2008 7:55 pm
by josh
If I were you I'd seek resources on the concepts, rather then getting hung up on other peoples implementations. There's a lot to be said for thinking for yourself. I'm just saying if your goal is to get a better grasp on inversion of control you're better off reading some philosophical literature on the subject, rather than deducting your own explanation on why the designer of the framework chose to do method A over method B. It's hard ( or even impossible, depending on your philosophical beliefs ) to deduce cause from effect, in other words its "statistically impossible" to figure out the real reason behind the code. You might read it and understand what it does, but it is my personal belief this is a false understanding. Just saying....