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.