简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM