Plugin Hooks - Ideas/help wanted

Discussion in 'PHP' started by blueparukia, Feb 13, 2008.

  1. #1
    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
     
    blueparukia, Feb 13, 2008 IP
  2. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #2
    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 :(
     
    blueparukia, Feb 15, 2008 IP
  3. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    decepti0n, Feb 15, 2008 IP
    blueparukia likes this.
  4. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #4
    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 ;)
     
    blueparukia, Feb 15, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    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:
     
    nico_swd, Feb 15, 2008 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    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
     
    blueparukia, Feb 15, 2008 IP
  7. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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
     
    decepti0n, Feb 15, 2008 IP
  8. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #8
    Yeah I have extended it a little bit, and I am commenting it myself - so I understand it much better. Thanks again.
     
    blueparukia, Feb 15, 2008 IP