【问题标题】:Call Function From One Function To Another In A Class PHP在类 PHP 中从一个函数调用函数到另一个函数
【发布时间】:2015-04-04 16:00:34
【问题描述】:

我想在另一个函数中使用我班级内部的一个函数。我试过只是调用它,但它似乎不起作用。这是我正在做的事情:

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

【问题讨论】:

标签: php oop


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多