Friedrich
Friedrich

Reputation: 2290

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?

Upvotes: 0

Views: 1318

Answers (3)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

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

Upvotes: 5

Bud Damyanov
Bud Damyanov

Reputation: 31829

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();
    }
}

Upvotes: 0

BenM
BenM

Reputation: 53198

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();
    }
}

Upvotes: 1

Related Questions