1

Okay, I have given up on figuring this out. I am creating a simple hook system, files are placed into a folder called hooks/.

Here is an example hook

<?php
//Testhook
function exampleHook() {
    echo 'Example hook as been run';
}
//Register hook (start is the hook type (where to be called), exampleHook is the function //name and TRUE if it is enabled or not.

$hooks->RegisterHook('start','exampleHook','TRUE');
?>

This system will load all the functions into the page, they wont be executed just yet though.

Now in the actual page heres what the code looks like:

<?php
    include ("hooks.class.php");
    $hooks = new hooks('hooks/');
?>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Hooks</title>
</head>

<body>
    <p>Start hooks should run below</p>
    <?php $hooks->callHooks('start'); ?>
    <p>Start hooks should now finish</p>
</body>
</html>

The callHooks function will execute any functions with the type 'start' in it. My problem is that in the testHook.php it cannot find the class $hooks, I've put the exampleHook into the same page and registered the function and it works perfectly but for some reason it cannot find the class $hooks even though the page is included.

Here is the hooks class file:

<?php

class hooks {

    //Variable Initialisation
    private $hookDir;
    private $allHooks;
    public $debug = FALSE;
    private $debugDetails = array();

    //Function runs when the class is called, sets the location and calls the function retrieveHooks() to include all the files
    function __construct($hookDir = "library/addHooks/hooks/") {
        $this->hookDir = $hookDir;
        $this->allHooks = array();
        $this->retrieveHooks();
    }

    public function retrieveHooks() {
        //Get all files located in hooks directory
        $hookFiles = scandir($this->hookDir); 
        //Loop through the files and remove any that arnt a file
        foreach($hookFiles as $hookFile) {
            if (strlen($hookFile) > 2) {
                //Include the file into the page
                include_once($this->hookDir . $hookFile);
                // START DEBUG SECTION OF CODE //
                if ($this->debug == TRUE) { $this->debugDetails[] = $this->hookDir . $hookFile . '<br />'; }
                // END DEBUG SECTION OF CODE //
            }
        }

    }

    //Function is called by hook files to register themselves into the class file
    public function registerHook($hookType,$functionName,$hookEnabled = TRUE) {
        if ($hookEnabled == TRUE) {
            $singleHook = array('type' => $hookType, 'function' => $functionName);
            $this->allHooks[] = $singleHook;
            // START DEBUG SECTION OF CODE //
            if ($this->debug == TRUE) { $this->debugDetails[] = ($singleHook); }
            // END DEBUG SECTION OF CODE //
        }
    }

    //Execute the hooks based on the hook type, the programmer can add as many hooks as he wants
    public function callHooks($hookType) {
        //For each hook in the array
        foreach($this->allHooks as $singleHook) {
            //If hook is found for that type eg. start, end, middle
            if ($singleHook['type'] == $hookType) {
                //Call the function that is now included within the web page
                call_user_func($singleHook['function']);
                // START DEBUG SECTION OF CODE //
                if ($this->debug == TRUE) { $this->debugDetails[] = ($singleHook); }
                // END DEBUG SECTION OF CODE //
            }
        }
    }

    //Return the array of debug details
    public function fetchDebugDetails() {
        return $debugDetails;   
    }

    //Test the class by outputting a simple message
    public function testClass() {
        $className = 'hooks';
        echo 'Class is working: ' . $className; 
    }

}

?>

Finally the error message:

Notice: Undefined variable: hooks in C:\wampserver64\www\Framework\library\addHooks\hooks\testHook.php on line 9

Fatal error: Call to a member function RegisterHook() on a non-object in C:\wampserver64\www\Framework\library\addHooks\hooks\testHook.php on line 9

Thanks, for any clarification just ask :) This has been really bugging me.

tereško
  • 58,060
  • 25
  • 98
  • 150
SamV
  • 7,548
  • 4
  • 39
  • 50
  • 1
    Note: `'TRUE'` is a string, you probably want to use a boolean: `true` – Markus Hedlund Jan 18 '11 at 20:19
  • I know, I have a variable that usually goes there with a boolean value but I just put that there for the sake of reducing code :) Thanks – SamV Jan 18 '11 at 20:24

2 Answers2

1

On the included file, instead of

$hooks->RegisterHook('start','exampleHook','TRUE');

use

$this->RegisterHook('start','exampleHook','TRUE');

This is because you include the file from the class

keepwalking
  • 2,644
  • 6
  • 28
  • 63
  • Thanks a lot man, will thank you in 4 minutes :) Could you explain in more detail why this works as I'm a bit confused? – SamV Jan 18 '11 at 20:23
0

@keepwalking is right, the problem is where the hook files are being included. You include the file inside the "retrieveHooks()" method in the "hooks" class, and that is where the code in the hook starts to execute. It doesn't have access to the "$hook" variable because it's in the wrong scope, but it does have access to the "$this" variable, which can be called to add the hook.

cmhudson
  • 1
  • 1