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

时间:2015-04-04 16:00:34

标签: php oop

我想在我的类中使用另一个函数中的函数。我试过只是调用它,但似乎没有用。这就是我在做的事情:

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.
    }
}

所以基本上我的问题是如何在一个类中调用另一个函数。谢谢,我是非常新的课程! :d

1 个答案:

答案 0 :(得分:1)

在PHP中,您总是需要使用$this->来调用类方法(或任何属性)。在您的情况下,代码是:

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

请注意,如果您将方法声明为静态,则必须使用self::static::

这是一个基本的PHP OOP语法,有关详细信息read the doc

相关问题