从一个函数调用函数到另一个类PHP
问题描述:
我想在另一个函数中使用我的类中的函数。我试图只是调用它,但它似乎并没有工作。这是我在做什么:从一个函数调用函数到另一个类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更多信息,read the doc
'$这个 - >信息($参数2);',做! ['Basic OOP PHP'](http://php.net/manual/en/language.oop5.basic.php) – Rizier123 2015-04-04 16:01:38
谢谢!我会阅读你发布的链接:) – 2015-04-04 16:03:44
不客气! – Rizier123 2015-04-04 16:04:11