- Joined
- Dec 17, 2008
- Messages
- 209
- Reaction score
- 31
I have always been in love with the idea of other people being able to extend my work without having to actually edit anything, which is why I've spent a lot of time trying to conjure up a plugin system that would fit my needs very nicely. Here's what I've come up with and I was wondering if some of the more advanced PHP coders could take a look, maybe make some improvements.
class.plugins.php:
plugin.testplugin.php:
test.php:
(Yes, I do realize the error in using print inside a class but this was merely for demonstration.)
class.plugins.php:
PHP:
<?php
class plugins {
private $plugins = array();
public function add($name, $path) {
include $path;
$this->plugins[$name] = new $name();
}
public function hook($hook, $params = array()) {
$hook = "hook_" . $hook;
foreach($this->plugins as $plugin) {
if(method_exists($plugin, $hook)) $plugin->$hook($params);
}
return true;
}
}
?>
plugin.testplugin.php:
PHP:
<?php
class testplugin {
public function hook_plugin_loaded($params = array()) {
print("Test plugin loaded!");
}
}
?>
test.php:
PHP:
<?php
include "class.plugins.php";
$plugins = new plugins();
foreach(glob("plugin.*.php") as $path) {
$name = substr(substr($path, 7), 0, -4);
$plugins->add($name, $path);
}
$plugins->hook("plugin_loaded");
?>
(Yes, I do realize the error in using print inside a class but this was merely for demonstration.)
Last edited: