Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Call to a member function on a non-object
    primarykey
    data
    text
    <p>Okay, I have given up on figuring this out. I am creating a simple hook system, files are placed into a folder called hooks/. </p> <p>Here is an example hook</p> <pre><code>&lt;?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-&gt;RegisterHook('start','exampleHook','TRUE'); ?&gt; </code></pre> <p>This system will load all the functions into the page, they wont be executed just yet though.</p> <p>Now in the actual page heres what the code looks like:</p> <pre><code>&lt;?php include ("hooks.class.php"); $hooks = new hooks('hooks/'); ?&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; &lt;title&gt;Hooks&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Start hooks should run below&lt;/p&gt; &lt;?php $hooks-&gt;callHooks('start'); ?&gt; &lt;p&gt;Start hooks should now finish&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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.</p> <p>Here is the hooks class file:</p> <pre><code>&lt;?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-&gt;hookDir = $hookDir; $this-&gt;allHooks = array(); $this-&gt;retrieveHooks(); } public function retrieveHooks() { //Get all files located in hooks directory $hookFiles = scandir($this-&gt;hookDir); //Loop through the files and remove any that arnt a file foreach($hookFiles as $hookFile) { if (strlen($hookFile) &gt; 2) { //Include the file into the page include_once($this-&gt;hookDir . $hookFile); // START DEBUG SECTION OF CODE // if ($this-&gt;debug == TRUE) { $this-&gt;debugDetails[] = $this-&gt;hookDir . $hookFile . '&lt;br /&gt;'; } // 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' =&gt; $hookType, 'function' =&gt; $functionName); $this-&gt;allHooks[] = $singleHook; // START DEBUG SECTION OF CODE // if ($this-&gt;debug == TRUE) { $this-&gt;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-&gt;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-&gt;debug == TRUE) { $this-&gt;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; } } ?&gt; </code></pre> <p>Finally the error message:</p> <pre><code>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 </code></pre> <p>Thanks, for any clarification just ask :) This has been really bugging me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload