I am looking to create a plugin manager like program which starts a loop that searches for .php files in the 'plugins' folder. I need this to somehow run a function called main() in each file which will then run other functions. How could i accomplish this without the other main() functions clashing and would there be any better alternative?
-
A better alternative, probably. What is exactly that you are trying to accomplish?noel– noel2012-09-21 23:59:54 +00:00Commented Sep 21, 2012 at 23:59
-
1You don't list what version of PHP, but i would suggest the use of Namespacing.Mike Mackintosh– Mike Mackintosh2012-09-22 00:00:18 +00:00Commented Sep 22, 2012 at 0:00
-
What i'm trying to create is a Paypal IPN like system which when it receives a request it can do whatever is included in plugins. for example, function main{ mail () } etc.Oliver Kucharzewski– Oliver Kucharzewski2012-09-22 00:02:54 +00:00Commented Sep 22, 2012 at 0:02
1 Answer
If you want to use functions then you can namespace them. But for something like this id use classes. for example each plugin might have a PluginConfiguration class which could either be namespaced like PluginName\PluginConfiguration or faked like PluginName_PluginConfiguration.
Then you could jsut instatiate these classes and invoke whatever for example:
class MyCool_Plugin implements PluginInterface {
// note the interface wouldnt be absolutely necessary,
// but making an interface or abstract class for this would be a good idea
// that way you can enforce a contractual API on the configuration classes
public function __construct() {
// do whatever here
}
public function main() {
// do whatever here
}
}
UPDATE:
By the way, what would 'PluginInterface' include?
Well an interface defines all methods (functions) a class must implement. You can use it to enforce a minimum API on any class the implements that interface. From your description this would be the method main although during development you may find that you need/want to add more.
Interface PluginInterface {
public function main();
}
You can also use type hinting to enforce a specific method signature. For example lets say you always want to inject the Application instance thats loading the plugin into the plugin itself so it can register things or set up additional stuff. In that case you might do:
Interface PluginInterface {
public function main(Application $app);
}