Ideal means to permit plugins for a PHP application

I am beginning a new internet application in PHP and also this moment around I intend to create something that individuals can expand by utilizing a plugin user interface.

Just how does one deal with creating 'hooks' right into their code to make sure that plugins can affix to details occasions?

2019-05-07 00:39:46
Source Share
Answers: 3

I think the most convenient means would certainly be to adhere to Jeff's very own suggestions and also look around existing code. Attempt considering Wordpress, Drupal, Joomla and also various other popular PHP - based CMS's to see just how their API hooks look. In this manner you can also get suggestions you might have not assumed of formerly to make points a little bit even more rubust.

An even more straight solution would certainly be to write basic documents that they would certainly "include_once" right into their documents that would certainly give the use they would certainly require. This would certainly be separated right into groups and also NOT given in one MASSIVE "hooks.php" documents. Take care however, due to the fact that what winds up taking place is that documents that they include wind up having an increasing number of dependences and also capability boosts. Attempt to maintain API dependences reduced. I.E less apply for them to include.

2019-05-08 20:44:26

The hook and also audience method is one of the most generally made use of, yet there are various other points you can do. Relying on the dimension of your application, and also that your mosting likely to permit see the code (is this mosting likely to be a FOSS manuscript, or something in residence) will certainly affect substantially just how you intend to permit plugins.

kdeloach has a wonderful instance, yet his execution and also hook function is a little harmful. I would certainly request for you to offer even more details of the nature of php application your writing, And just how you see plugins suitable in.

+1 to kdeloach from me.

2019-05-08 20:14:50

You can make use of an Observer pattern. A straightforward useful means to complete this :


/** Plugin system **/

$listeners = array();

/* Create an entry point for plugins */
function hook() {
    global $listeners;

    $num_args = func_num_args();
    $args = func_get_args();

    if($num_args < 2)
        trigger_error("Insufficient arguments", E_USER_ERROR);

    // Hook name should always be first argument
    $hook_name = array_shift($args);

        return; // No plugins have registered this hook

    foreach($listeners[$hook_name] as $func) {
        $args = $func($args); 
    return $args;

/* Attach a function to a hook */
function add_listener($hook, $function_name) {
    global $listeners;
    $listeners[$hook][] = $function_name;


/** Sample Plugin **/
add_listener('a_b', 'my_plugin_func1');
add_listener('str', 'my_plugin_func2');

function my_plugin_func1($args) {
    return array(4, 5);

function my_plugin_func2($args) {
    return str_replace('sample', 'CRAZY', $args[0]);


/** Sample Application **/

$a = 1;
$b = 2;

list($a, $b) = hook('a_b', $a, $b);

$str  = "This is my sample application\n";
$str .= "$a + $b = ".($a+$b)."\n";
$str .= "$a * $b = ".($a*$b)."\n";

$str = hook('str', $str);
echo $str;

Output :

This is my CRAZY application
4 + 5 = 9
4 * 5 = 20

Notes :

For this instance resource code, you have to proclaim all your plugins prior to the real resource code that you intend to be extendable. I've consisted of an instance of just how to take care of solitary or numerous values being passed to the plugin. The hardest component of this is creating the real documents which details what debates get passed to each hook.

This is simply one method of completing a plugin system in PHP. There are far better choices, I recommend you look into the WordPress Documentation for additional information.

Sorry, it shows up emphasize personalities are changed by HTML entities by Markdown? I can re - blog post this code when this bug obtains dealt with.

Modify : Nevermind, it just shows up in this way when you are editing and enhancing

2019-05-08 19:46:35