tag:blogger.com,1999:blog-865923359735383241.post2083540957332222626..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: Best way to allow plugins for a PHP applicationUnknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-865923359735383241.post-21832956841612835662012-06-01T22:34:29.525-06:002012-06-01T22:34:29.525-06:00Check:
http://framework.zend.com/manual/de/zend....Check:<br /><br /><br />http://framework.zend.com/manual/de/zend.loader.html<br />http://code.google.com/p/phpplexus/Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-54833014461946977462012-06-01T22:34:28.520-06:002012-06-01T22:34:28.520-06:00Definitely check out Drupal's method. Here'...Definitely check out Drupal's method. Here's some of their own documentation.<br />They use a pretty nice model where the module's name is used as a prefix to all exposed functions. A lot of the mechanics are automatic that way.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-4632155084006970632012-06-01T22:34:24.776-06:002012-06-01T22:34:24.776-06:00The hook and listener method is the most commonly ...The hook and listener method is the most commonly used, but there are other things you can do. Depending on the size of your app, and who your going to allow see the code (is this going to be a FOSS script, or something in house) will influence greatly how you want to allow plugins.<br /><br />kdeloach has a nice example, but his implementation and hook function is a little unsafe. I would ask for you to give more information of the nature of php app your writing, And how you see plugins fitting in. <br /><br />+1 to kdeloach from me.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-45533254650443491452012-06-01T22:34:23.583-06:002012-06-01T22:34:23.583-06:00You could use an Observer pattern. A simple funct...You could use an Observer pattern. A simple functional way to accomplish this:<br /><br /><?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;?><br /><br />Output:<br /><br />This is my CRAZY application4 + 5 = 94 * 5 = 20<br /><br />Notes:<br /><br />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.<br /><br />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.<br /><br />Sorry, it appears underscore characters are replaced by HTML entities by Markdown? I can re-post this code when this bug gets fixed.<br /><br />Edit: Nevermind, it only appears that way when you are editingUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com