Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 30655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:28:55+00:00 2026-05-10T13:28:55+00:00

I am starting a new web application in PHP and this time around I

  • 0

I am starting a new web application in PHP and this time around I want to create something that people can extend by using a plugin interface.

How does one go about writing ‘hooks’ into their code so that plugins can attach to specific events?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:28:56+00:00Added an answer on May 10, 2026 at 1:28 pm

    You could use an Observer pattern. A simple functional way to accomplish this:

    <?php  /** 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);      if(!isset($listeners[$hook_name]))         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 example source code, you must declare all your plugins before the actual source code that you want to be extendable. I’ve included an example of how to handle single or multiple values being passed to the plugin. The hardest part of this is writing the actual documentation which lists what arguments get passed to each hook.

    This is just one method of accomplishing a plugin system in PHP. There are better alternatives, I suggest you check out the WordPress Documentation for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer An option would be to check for the presence of… May 11, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer I doubt they work any differently on other OS installations… May 11, 2026 at 7:21 pm
  • Editorial Team
    Editorial Team added an answer You can use a 'varbinary(MAX)' or 'image' column type. Linq2Sql… May 11, 2026 at 7:21 pm

Related Questions

I am starting to build a new web application that will require user accounts.
Concerning pages that build a web application: Lately, I have found myself creating web
I have been looking into using Facebook Connect for a new web site I
I am building web applications using Ruby on Rails and I would like to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.