2

The following code gives me the erorFatal error: Call to undefined function display() in /srv/http/recipes/classes/controller.php on line 4 when I try to access the page

class Controller{
    function display($template){
        display('error');
    }
}
4
  • 2
    You're not trying to call a global function, you're trying to call a class method, so use $this: class Controller{ function display($template){ $this->display('error'); } } Commented May 21, 2015 at 11:07
  • 2
    Now sits back and waits for the stackoverflow from the recursive call Commented May 21, 2015 at 11:09
  • @MarkBaker made my day, of course this function causes an stackoverflow error, I just cut the unecessary parts, now it works. Commented May 21, 2015 at 11:10
  • also refer self vs $this Commented May 21, 2015 at 11:13

3 Answers 3

4

Change code to this :

function display($template) {

   $this->display('error');
}
Sign up to request clarification or add additional context in comments.

Comments

2

You have to use $this or self -

class Controller{
    function display($template){
        $this->display('error');
    }
}

Or use - self::display('error');

Comments

2
public function display($template) {

   $this->display('error');
}

Comments

Your Answer

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