dpacmittal Posted March 14, 2010 Share Posted March 14, 2010 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 Quote Link to comment Share on other sites More sharing options...
herghost Posted March 14, 2010 Share Posted March 14, 2010 I believe wordpress uses the smarty template engine, it might be worth while checking that out. Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted March 14, 2010 Author Share Posted March 14, 2010 Nope it does not. Even if it did, what does it have to do with plugin system? Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted March 15, 2010 Author Share Posted March 15, 2010 Bump. If I don't find help on phpfreaks, I won't find it anywhere. Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2010 Share Posted March 15, 2010 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 Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted March 15, 2010 Author Share Posted March 15, 2010 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? Quote Link to comment Share on other sites More sharing options...
herghost Posted March 15, 2010 Share Posted March 15, 2010 Be Nope it does not. Even if it did, what does it have to do with plugin system? Because the function $hook is used a lot in smarty, it lets you place blocks in various places by using the $hook parameter Quote Link to comment Share on other sites More sharing options...
simshaun Posted March 15, 2010 Share Posted March 15, 2010 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); Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted March 15, 2010 Author Share Posted March 15, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.