First of all, I know this a very complex script and don't expect anyone to give me the completed script. I just need pointers on how to acomplish it, and maybe some sample code . I have known since I thought of this that it is way out of my knowledge. Lets dive straight into the good stuff. Here is the code I want to work: <?php //Define the class - no idea what to call it, so I'll just use ClassName :p $plugin = new Hooks; //Set the hook $plugin->set_hook("awesome","hook()"); function hook() { echo "nice day"; } echo "Hello world, it is a "; //Uses the hook "awesome" - which runs the function hook() $plugin->use_hook("awesome"); ?> PHP: The output of that would be: Hello world, it is a nice day. Currently, all I have in my class is: <?php class Hook{ function set_hook($hookname,$function){} function use_hook($hookname){} } ?> PHP: Obviously that does crap all. There are many, many things I don't get in this - I have no idea what functions to use, or how to use them, whether regex or arrays will be involved, or how to convert the $function from a string to a function. Any basic examples would help me majorly, and anyone who does manage to help me shall receive a link to their website in the "Major Contributor" section of the script ACP - it will only be a script shared to a select few (CMS), but it may help you out with a few things. Now, hooks a hard for me to explain, I understood them, and figured out how useful they would be to have in my script - which may or may not be tightly integrated with MyBB. So, I really need help from very good coders now, as I am (for the first time ever) - so lost I can't even think of anything to try. BP
Well I have consulted with people, and I do not think this is possible for me to do at all, as t involves array, calling functions from that array etc etc. Probably around 40-50 lines of code in total...but complicated. This does annoy me greatly. I have never had a code so hard that I can't even think about how to do it
Try this : <?php class Hook{ function set_hook($hookname,$function){ $this->$hookname = $function; } function use_hook($hookname){ call_user_func($this->$hookname); } } $plugin = new Hook; //Set the hook // Note : dont have brackets for the function name $plugin->set_hook("awesome","hook"); function hook() { echo "nice day"; } echo "Hello world, it is a "; //Uses the hook "awesome" - which runs the function hook() $plugin->use_hook("awesome"); ?> PHP: Worked for me
Seriously though? That easy? I was looking at complex arrays and using call_user_func_array. This simplifies things a lot. There is a bug though, if the hook does not exist, you get this error: Warning: call_user_func() [function.call-user-func]: First argument is expected to be a valid callback in W:\www\cms\admin\test.php on line 9 Code (markup): . I am off to be now, though I'll leave you some nice, shiny green rep first
Of course you get an error. How do you want to use a function that doesn't exist? If you want to check if it exists, you can do: if (function_exists($this->hookname)) { call_user_func($this->hookname); } else { // Handle error } PHP:
Yeah I know that. Though technically the function exists, but the hook doesn't Either way, I can troubleshoot this by myself now. Thanks all BP
Apparently, I've never done something like it until just now, and actually, it helped me too. Make it a bit friendlier like nico said though
Yeah I have extended it a little bit, and I am commenting it myself - so I understand it much better. Thanks again.