1

I am trying to make a own class in CodeIgniter and therefore I have made a file in the folder

libraries

This class is called

Menu

And it contains a class to create a navigation bar for my website. this class is like

class Menu {

    public function draw($menu) {
        $CI =& get_instance();

        $output = '';
        foreach ($menu as $key => $value) {
            $output .= "<li>";

            if (is_array($value)) {

                if (strpos($key, '|') !== false) {
                    $param = explode('|', $key);
                    $output .= anchor($param[1], $param[0]);
                } else {
                    $output .= anchor('#', $key);
                }

                $output .= PHP_EOL."<ul>".PHP_EOL;
                $output .= draw($value);
                $output .= "</ul>".PHP_EOL."</li>".PHP_EOL;
            } else {
                $output .= anchor($key, $value, $CI->uri->slash_segment(1, 'leading') == $key ? 'class="active"' : '');
                $output .= "</li>".PHP_EOL;
            }
        }
        return $output;
    }
}

I have put this class in my config file as autoload under libraries

$autoload['libraries'] = array('menu');

When I call the class to use it I do this

    <?php 
    $m = new Menu();
    echo $m->draw($menu);
    ?>

But unfortunately I get this error

Fatal error: Call to undefined function draw() in /Users/username/Sites/infinity2.0/application/libraries/Menu.php on line 22

Line 22 is $output .= draw($value);

But I don't know how to solve this I think its because of calling its own method again.. any help is welcome and appreciated. :)

4

1 回答 1

3

改变

$output .= draw($value);

$output .= $this->draw($value);

$this引用当前对象,所以如果你想draw()在自身内部递归调用,你需要使用它来引用它..

您可能想阅读 php.net 网站的说明:http: //php.net/manual/en/language.oop5.basic.php

于 2013-08-06T22:22:55.917 回答