1

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');
    }
}
  • 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'); } }` – Mark Baker May 21 '15 at 11:07
  • 2
    Now sits back and waits for the stackoverflow from the recursive call – Mark Baker May 21 '15 at 11:09
  • @MarkBaker made my day, of course this function causes an stackoverflow error, I just cut the unecessary parts, now it works. –  May 21 '15 at 11:10
  • also refer [self vs $this](http://stackoverflow.com/q/151969) – viral May 21 '15 at 11:13

3 Answers3

3

Change code to this :

function display($template) {

   $this->display('error');
}
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
1

You have to use $this or self -

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

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

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1
public function display($template) {

   $this->display('error');
}
Milap Jethwa
  • 471
  • 4
  • 7