2015-04-04 215 views
0

我想在另一個函數中使用我的類中的函數。我試圖只是調用它,但它似乎並沒有工作。這是我在做什麼:從一個函數調用函數到另一個類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

+4

'$這個 - >信息($參數2);',做! ['Basic OOP PHP'](http://php.net/manual/en/language.oop5.basic.php) – Rizier123 2015-04-04 16:01:38

+1

謝謝!我會閱讀你發佈的鏈接:) – 2015-04-04 16:03:44

+0

不客氣! – Rizier123 2015-04-04 16:04:11

回答

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