0

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?

Friedrich
  • 2,211
  • 20
  • 42
  • 1
    `func1()` refers to the *global* function `func1()`. Not the `func1()` of the current class. To call the `func1()` in the current class you'd use `$this->func1()` (or `self::func1()` if it's a static method) – h2ooooooo Apr 29 '14 at 10:46

3 Answers3

5
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

Community
  • 1
  • 1
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
1

You're using OO techniques, so you'll have to use the $this keyword to access the func1() function:

class testclass
{
    function func1()
    {
        return "hello";
    }
    function func2()
    {
        echo $this->func1();
    }
}
BenM
  • 52,573
  • 26
  • 113
  • 168
0

You have error in your code, you cannot invoke the function like this, the proper way is by using $this (which refers to the class itself):

class testclass
{
    function func1()
    {
        return "hello";
    }
    function func2()
    {
        echo $this->func1();
    }
}
Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52