简体   繁体   English

调用类方法时,php未定义变量错误

[英]php undefined variable error when calling class method

I have been writing procedural php for years and am very comfortable with it. 我已经写了多年的程序性PHP,并且对它很满意。 Recently I decided to rework an existing site and switch to PDO and OOP. 最近我决定重新修改现有站点并切换到PDO和OOP。 Everyone is telling me that this is a better way to go but the learning curve is killing me. 每个人都在告诉我这是一个更好的方法,但学习曲线正在扼杀我。 When trying to call a class, I get the following. 当试图打电话给我时,我得到以下内容。

Menu Builder 菜单生成器

vbls: 5 1 vbls:5 1

Notice: Undefined variable: Menu in /home/lance/DallyPost/projectWebSite/trunk/1/core/modules/menuBuilder.php on line 9 注意:未定义的变量:第9行的/home/lance/DallyPost/projectWebSite/trunk/1/core/modules/menuBuilder.php中的菜单

Fatal error: Call to a member function menuFramework() on a non-object in /home/lance/DallyPost/projectWebSite/trunk/1/core/modules/menuBuilder.php on line 9 致命错误:在第9行的/home/lance/DallyPost/projectWebSite/trunk/1/core/modules/menuBuilder.php中调用非对象的成员函数menuFramework()

The procedure is that I have included menu.php at the top of index.php, prior to including the following script: 在包含以下脚本之前,我已经将index.php包含在index.php的顶部:

<?php
//menuBuilder.php
echo"<h2>$pageTitle</h2>";
$pub = $_URI_KEY['PUB'];
$dir = $_URI_KEY['DIRECTORY'];

echo"vbls: $pub $dir";

if($Menu->menuFramework("$pub", "$dir") === false) {
    echo"The base menu framework failed to build correctly.";
}
else{
    echo"<p>The base menu framework has been successfully constructed.</p>";
}
?>

As you can see, the above script calls a method in the Menu class: 如您所见,上面的脚本调用Menu类中的方法:

<?php
//menu.php
class Menu{
    private $db; 
    public function __construct($database) {
        $this->db = $database;
    }

    public function menuFramework($pub, $directory){
        $link = "/" . $directory . "/index.php/" . $pub . "/home/0/Home-Page/";
        $inc = "core/menus/" . $pub . "category.php";
        $file = "core/menus/" . $pub . "menuFramework.php";

        $text = "<nav class=\"top-bar\" data-topbar>";
        $text .= "<ul class=\"title-area\">";
        $text .= "<li class=\"name\">";
        $text .= "<h1><a href=\"$link\">Home Page</a></h1>";
        $text .= "</li>";
        $text .= "</ul>";
        $text .= "include($inc)";
        $text .= "</nav>";

        //write text to a file
        if(file_put_contents($file, $text)){
            return true;
        }
        else{
            return false;
        }
    }
    ... rest of file not shown

Can you help me understand why I am getting this error. 你能帮我理解为什么我会收到这个错误。 My understanding is that the variable was or should have been defined when I included menu.php, which was done before which was called a the top if index.php 我的理解是,当我包含menu.php时,该变量已经或应该被定义,之前已经完成,如果是index.php则称为top。

Thanks 谢谢

将其添加到脚本的顶部

$menu = new Menu($dbHandle);

That's not how classes work. 这不是班级的工作方式。 You can't reference an entire class, Menu , with a variable, $Menu , to invoke instance methods. 你不能引用整个类, Menu ,用变量$Menu ,调用实例方法。

You need to create an instance of your class on which to invoke methods: 您需要创建一个类的实例来调用方法:

$menu = new Menu(...);

You can create class-level "static" methods which are invoked on the class itself, but that syntax doesn't involve $Menu . 您可以创建在类本身上调用的类级“静态”方法,但该语法不涉及$Menu You would use Menu::method_name() for that. 你可以使用Menu::method_name()

You are calling $Menu-> so your are a calling a variable called Menu, instead of the class Menu. 您正在调用$ Menu->所以您调用的是一个名为Menu的变量,而不是类Menu。 anyways, that function is not static, so you need to instantiate an object. 无论如何,该函数不是静态的,因此您需要实例化一个对象。

For that, add a this line: 为此,添加一行:

 $menu = new Menu($db);

where $db is your database object, if you really need it, or null if you dont (i cannot say with that code fragment) 其中$ db是你的数据库对象,如果你真的需要它,或者如果你不这样做是null(我不能用那个代码片段说)

and then call 然后打电话

$menu->menuFramework(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM