Jump to content

Plugin (hook) system in PHP


dpacmittal

Recommended Posts

I want to create a plugin system similar to one implemented in wordpress. Google hasn't been of any help. I tried reading wordpress source but it would take much time to understand the underlying concept. I've checked its database and it doesn't store any plugin information in the database which means that it parses all the plugins everytime the page is loaded which, in my opinion, is not (?) a good way to do it.

 

The system they've used is the hook system. There are hooks in most of the places. You add some action to the hook and when the hook is called, the action to attached is also executed. For eg; if you attach an action of cleaning a certain folder on the hook of add-post (say), everytime you add a post, the folder will be cleared.

 

Okay, so I've not been able to understand how wordpress does it. However, I myself have a basic idea for this. I just want opinions if it is a good way to do it. I've thought of something like this:

 

Plugins would have to call a function to attach an action to hook eg;

attach_action ($hookname, $callback)

 

When the plugin is installed, attach_action would add this information to the database.

 

When the hook is called, it fetches all the actions attached to it from the database and executes them.

 

When the plugin is uninstalled, the record from database is removed.

 

 

So this is it. I want to know if this is better/worse way than how wordpress does? Thanks

Link to comment
Share on other sites

You might want to look into the Observer pattern. There's plenty of examples around but this one was the first simple one I found googling 'php obeserver'

 

http://devzone.zend.com/article/4284

Thanks. I'll check out the link. :)

In the meanwhile, could you tell my if the idea which I had thought initially in the original post is a good one?

Link to comment
Share on other sites

My own plugin implementation is very similar to what you have described in your original post, and is also an implementation of the observer pattern as thorpe referenced.

 

The concept is simple, although the terminology I use is slightly different.  There are two primary pieces, 1. events and 2. listeners that attach to events.

Each "plugin" can be extremely simple or extremely complex, but either way it adds at least one listener to an event. (e.g. addListener('page.init', 'functionToRunOnPageInit');)

 

Handling the listeners attached to events can be done two ways.

1. Notify listeners that an event has occurred.  This simply runs the listeners attached to an event. notifyListeners('page.init');

2. Let the listeners filter or change a value.  The order in which the value is modified is determined by the order in which listeners were added to the event. $aValueThatWillBeChanged = runFilters('page.init', $aValueThatWillBeChanged);

Link to comment
Share on other sites

You might want to look into the Observer pattern. There's plenty of examples around but this one was the first simple one I found googling 'php obeserver'

 

http://devzone.zend.com/article/4284

Thanks, it gave me the idea.

 

My own plugin implementation is very similar to what you have described in your original post, and is also an implementation of the observer pattern as thorpe referenced.

 

The concept is simple, although the terminology I use is slightly different.  There are two primary pieces, 1. events and 2. listeners that attach to events.

Each "plugin" can be extremely simple or extremely complex, but either way it adds at least one listener to an event. (e.g. addListener('page.init', 'functionToRunOnPageInit');)

 

Handling the listeners attached to events can be done two ways.

1. Notify listeners that an event has occurred.  This simply runs the listeners attached to an event. notifyListeners('page.init');

2. Let the listeners filter or change a value.  The order in which the value is modified is determined by the order in which listeners were added to the event. $aValueThatWillBeChanged = runFilters('page.init', $aValueThatWillBeChanged);

Thanks, it helped quite a lot.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.