簡體   English   中英

在類PHP中從一個函數調用另一個函數

[英]Call Function From One Function To Another In A Class PHP

我想在另一個函數中使用類內部的函數。 我試過只是調用它,但它似乎不起作用。 這是我在做什么:

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中,您始終需要使用$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語法,有關更多信息, 請閱讀doc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM