简体   繁体   English

在类PHP中从一个函数调用另一个函数

[英]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 :d

In PHP you always need to use $this-> to call a class method (or any attribute). 在PHP中,您始终需要使用$this->来调用类方法(或任何属性)。 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:: . 请注意,如果您将方法声明为静态,则必须使用self::static::

This is a basic PHP OOP syntax, for more information read the doc 这是一种基本的PHP OOP语法,有关更多信息, 请阅读doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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