如何调用一个类的方法? [英] How to call a method of a class?
本文介绍了如何调用一个类的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个类中有2个公共函数,必须在同一个类中使用不同的参数调用1个私有函数...由于某种原因,它告诉我找不到该函数...
I got 2x public functions in a class that must call 1 private function with different parameters also in the same class... for some reason it tell me that it can't find the function...
示例:
class Foo {
private function Do(...)
{
....
return $whatever;
}
public function One(...)
{
return Do(...);
}
public function Two(...)
{
return Do(...);
}
}
出现错误:
致命错误:在...中调用未定义的函数do()
Fatal error: Call to undefined function do() in ...
推荐答案
You have to use $this
to refer to the instance and the T_OBJECT_OPERATOR
to access/mutate/call members/methods of an instance, e.g.
$this->do();
请通过
- Chapter on Classes and Objects in the PHP Manual and
- What is the point of having $this and self:: in PHP?
这篇关于如何调用一个类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文