简体   繁体   中英

php undefined variable error when calling class method

I have been writing procedural php for years and am very comfortable with it. Recently I decided to rework an existing site and switch to PDO and 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

Notice: Undefined variable: Menu in /home/lance/DallyPost/projectWebSite/trunk/1/core/modules/menuBuilder.php on line 9

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

The procedure is that I have included menu.php at the top of index.php, prior to including the following script:

<?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:

<?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

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.

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 . You would use Menu::method_name() for that.

You are calling $Menu-> so your are a calling a variable called Menu, instead of the class 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)

and then call

$menu->menuFramework(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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