0

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

1
  • when you say "My understanding is that the variable was or should have been defined when I included menu.php" i guess you will solve this issue, but you will get many others. I recommend you to read some tutorials about OOP in general, and then about OOP in php. including a class file does nothing, just telling the system where that class can be found. but after that if you want to use an object you have to instantiate it. This makes sense, since probably you can need more than one object, and this is why you create a class, so you have a way to encapsulate functionality Commented Dec 5, 2013 at 4:57

3 Answers 3

1

add this at the top of the script

$menu = new Menu($dbHandle);
Sign up to request clarification or add additional context in comments.

2 Comments

You must have to autoload that class or require_once, so all the code is available to use in your code. $Menu = new Menu($dbHandle); $Menu not $menu both are different.
yes thats right what a faulty eye i have even wear a spectacles
1

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.

Comments

0

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(...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.