Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Plugin Hooks for a PHP application
blueparukia Offline
Junior Member
**
Posts: 35
Joined: Jan 2008
Post: #1
Plugin Hooks for a PHP application
First - I am not expecting anyone to do this for me, I want to know and understand HOW to do it. I want to have plugins hooks in my CMS almost exactly the same as MyBB - except without the extra optional attributes (priority etc.).

I have considered just copying MyBB's plugin and cache class - but I would not understand how I did it, and that is crucial for me.


Still, I have absolutely no idea how to do it, so need help on what I should do.

Of course, anyone who helps will be credited fully as the creator of the plugin system in the script ACP.

Cheers,

BP
02-14-2008 04:18 PM
Find all posts by this user
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: Plugin Hooks for a PHP application
The idea of hooks is basically to execute a custom function at a certain point in time.

There are in fact many possible different ways this can be implemented.
If you want a MyBB like approach, where a plugin adds hooks, and the main script executes them, all you need to do is provide something to store the hooks (hint, an array), and something which will loop through the list and execute them.

My Blog
02-14-2008 08:03 PM
Find all posts by this user
blueparukia Offline
Junior Member
**
Posts: 35
Joined: Jan 2008
Post: #3
RE: Plugin Hooks for a PHP application
I do like the MyBB approach...Wink

Ok, so I am still going to be confused about how to go about it, but an array is a good idea. So lets say I'll use an array, here is what I'd have in my class:

PHP Code:
function add_hook($hook,$function){
$var = array(
hook_name => $hook,
function_name => $function
}


For run hooks, I do not how I'd loop through it, as both for and while loops would not work here, nor do I knw how I would execute the $var_function_name as a variable. I know there is a function for something similar, (call_user_func_array)- but I highly doubt I'd be able to use it on something like this until after a few weeks/months of practice, and I need this ASAP,

Cheers and thanks,

Josh

(This post was last modified: 02-14-2008 09:18 PM by blueparukia.)
02-14-2008 09:13 PM
Find all posts by this user
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #4
RE: Plugin Hooks for a PHP application
Umm, I'm sure you know about scope?  Your array must be visible to both add hook and run hook functions.

Note that you need to add to the array, not assign to it.
As for running them, you can simply call them, ie:

PHP Code:
$f = 'myfunction';
$f();

// above is equivalent to
myfunction();


My Blog
02-15-2008 07:11 PM
Find all posts by this user
blueparukia Offline
Junior Member
**
Posts: 35
Joined: Jan 2008
Post: #5
RE: Plugin Hooks for a PHP application
Yeah I know about scope. However I just can't write out correct code in a forum post, I need to concentrate Tongue

As for adding to an array, I have no idea how to really do this at all...it may just be asier to fork out some money to have it done.

Thanks for your help, I am going to have a try tonight, and if I don't get anywhere, I'll just pay a dev Tongue

Josh
02-15-2008 07:41 PM
Find all posts by this user
blueparukia Offline
Junior Member
**
Posts: 35
Joined: Jan 2008
Post: #6
RE: Plugin Hooks for a PHP application
Hmmm. I have thought about doing this SQL based....with a table structure like:

Function Name|File Name|Hook_Name

Even so - executing it would still cause me no end of problems.

This is the only piece of code I have ever had absolutely no idea what the f**k I am doing. I am not used to it, and I can not stand it, and my friggin pride will not let me pay for a coder. I am seriously about to rip something out of the computer and hurl it at something.
02-15-2008 08:22 PM
Find all posts by this user
blueparukia Offline
Junior Member
**
Posts: 35
Joined: Jan 2008
Post: #7
RE: Plugin Hooks for a PHP application
Easy enough actually. Thanks for your help though, the base of the code:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?
class Hook{

    function set_hook($hookname,$function){
        $this->$hookname = $function;
    }
    
    function use_hook($hookname){
       if (function_exists($this->$hookname))
	{
    call_user_func($this->$hookname);
	}
	else
	{
    	
	}
    }

}
?>


Much simple than I would have thought Ouch

(This post was last modified: 02-15-2008 10:13 PM by blueparukia.)
02-15-2008 10:00 PM
Find all posts by this user
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #8
RE: Plugin Hooks for a PHP application
That code has a number of problems, unfortunately, primarily with multiple hooks...
Instead of telling you, I'll let you figure it out.

My Blog
02-16-2008 11:05 AM
Find all posts by this user
blueparukia Offline
Junior Member
**
Posts: 35
Joined: Jan 2008
Post: #9
RE: Plugin Hooks for a PHP application
Yeah I can figure it out by looking he code. It'll either be an "already defined function" error, or just execute the last hook.

Are arrays the only way to do this?
02-16-2008 10:47 PM
Find all posts by this user
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #10
RE: Plugin Hooks for a PHP application
blueparukia Wrote:Are arrays the only way to do this?
No, but it's the most logical solution.

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function add_a_hook($hook, $function)
{
 global $plugin_hooks;
 $plugin_hooks[$hook][] = $function;
}

function run_a_hook($hook)
{
 global $plugin_hooks;
 foreach($plugin_hooks[$hook] as $function)
 {
  $function();
 }
}

If you're really determined to do PHP scripting, you should really understand the above concept.


My Blog
(This post was last modified: 02-16-2008 11:36 PM by ZiNgA BuRgA.)
02-16-2008 11:35 PM
Find all posts by this user

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: