doushenyu2537 2011-01-18 20:12
浏览 57
已采纳

PHP调用非对象的成员函数[重复]

This question already has an answer here:

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.

</div>
  • 写回答

2条回答 默认 最新

  • duanmei4362 2011-01-18 20:19
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大