michael jones
michael jones

Reputation: 740

Call Function From One Function To Another In A Class PHP

I would like to use a function from inside my class in another function. I have tried just calling it but it does not seem to work. Here is what I am doing:

class dog {
    public function info($param) {
        //Do stuff here
    }
    public function call($param2) {
        //Call the info function here
        info($param2);
        //That does not seem to work though, it says info is undefined.
    }
}

So basically my question is how do I call a function from another in a class. Thank You, I am VERY new to classes! :D

Upvotes: 0

Views: 966

Answers (1)

dynamic
dynamic

Reputation: 48141

In PHP you always need to use $this-> to call a class method (or any attribute). In your case the code is:

public function call($param2) {
        //Call the info function here
        $this->info($param2);
        //That does not seem to work though, it says info is undefined.
}

Please note that If you declare your method as static, then you will have to use either self:: or static::.

This is a basic PHP OOP syntax, for more information read the doc

Upvotes: 1

Related Questions