0

I am trying to make a dynamic menu, but I keep getting the fatal error. Here is the code:

class Menu {

public $menu;

function __contstruct() {
    $this -> menu = array("Home" => "index.php",
    //"Eat" => array("Casual" => "casual.php", "Fine dining" => "fine_dining.php"),
    "Contact" => "contact.php");
}

public static function page_name() {
    return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}

public static function menu_list() {
    $menu_list = "";
    foreach ($this->menu as $name => $url) {
        echo "<li ";
        if ($url == $this -> pagename()) {
            $menu_list .= "class='active'";
        }
        $menu_list .= "><a href='";
        $menu_list .= $url;
        $menu_list .= "'>" . $name . "</a></li>";
        return ($menu_list);
    }
}

}
?>

and calling it with

$nav = new Menu();
echo $nav->menu_list();

Please help me figure why it isn't working.

3
  • What is the exact error you are getting? Commented Sep 11, 2014 at 16:32
  • You've got multiple typos anyways. $this->pagename() does not exist. you've got $this->page_name() defined. Commented Sep 11, 2014 at 16:32
  • 1
    __contstruct() <= look closely. Zoom in if you have to. Better yet, look inside a dictionary. Commented Sep 11, 2014 at 16:33

1 Answer 1

2

You can't use $this in a static method. $this is for objects. Use self to refer to the class a method is contained in when you don't have an instance.

Remove static from you method signature if you want to use in object context.

And more importantly, you spelled 'construct' incorrectly and typed 'pagename' instead of 'page_name'. This works:

<?php

class Menu {

public $menu;

function __construct() {
    $this -> menu = array("Home" => "index.php",
    //"Eat" => array("Casual" => "casual.php", "Fine dining" => "fine_dining.php"),
    "Contact" => "contact.php");
}

public function page_name() {
    return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}

public function menu_list() {
    $menu_list = "";
    foreach ($this->menu as $name => $url) {
        echo "<li ";
        if ($url == $this -> page_name()) {
            $menu_list .= "class='active'";
        }
        $menu_list .= "><a href='";
        $menu_list .= $url;
        $menu_list .= "'>" . $name . "</a></li>";
        return ($menu_list);
    }
}

}

$nav = new Menu();
echo $nav->menu_list();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. That works perfectly. I'm trying to get the handle of OOP and this is my project to do it with. I really appreciate the help.
@user2367438 Please mark as accepted answer by clicking the checkmark. An upvote is always nice, too = )
Not enough rep for an upvote, but it will be accepted. Thanks again

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.