Search code examples
phpfunctionundefinedscopes

Call to undefined function with PHP


I have this class:

class testclass{
    function func1(){
        return "hello";
    }
    function func2(){
        echo func1();
    }
}

When I am running

$test = new testclass();
$test->func2();

I get an error: Fatal error: Call to undefined function func1() with the line index of echo func1();

My question now is, how do I make the func2 recognize func1

Is this a problem with the scopes?


Solution

  • function func2(){
            echo func1();
        }
    

    should be

    function func2(){
            echo $this->func1();
        }
    

    http://www.php.net/manual/en/language.oop5.visibility.php

    self:: vs className:: inside static className metods in PHP