Plug-In System?
Moderator: General Moderators
-
psychotomus
- Forum Contributor
- Posts: 487
- Joined: Fri Jul 11, 2003 1:59 am
Plug-In System?
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?
nebody?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Plug-In System?
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:
File bar.php contains:
To run them:
File foo.php contains:
Code: Select all
class foo {
public function run(myparam$);
// my code here
}
}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)