Page 1 of 1

Plug-In System?

Posted: Thu Apr 02, 2009 9:55 pm
by psychotomus
heres what I want to do. Instead of editing files I would like to have some sort of plug-in system. I can think of only one way to do this and that would be ot include everyfile in a DIR then call that same function (they would have same # of paramaters) but the displays a nameforce error. how can I do this?

Re: Plug-In System?

Posted: Fri Apr 03, 2009 6:09 pm
by psychotomus
nebody?

Re: Plug-In System?

Posted: Fri Apr 03, 2009 6:35 pm
by Christopher
This is essentially the Command Pattern. Each file in your directory has a class of the same name in it. each class has a common method that can be called. This is one form of polymorphism which is a foundation of OO. For example:

File foo.php contains:

Code: Select all

class foo {
    public function run(myparam$);
         // my code here
    }
}
File bar.php contains:

Code: Select all

class bar {
    public function run(myparam$);
         // my code here
    }
}


To run them:

Code: Select all

foreach (glob('mydir/*.php') as $name) {
     include $name;
     $class = str_replace('.php', '', $name);
     $obj = new $class()
     $obj->run($myparam);
}