Plug-In System?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Plug-In System?

Post 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?
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: Plug-In System?

Post by psychotomus »

nebody?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Plug-In System?

Post 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);
}
(#10850)
Post Reply