当我从我的班级调用本地方法时,如下面的示例所示,是否必须在其之前放置$this->
?在类中调用本地方法时需要'this'吗?
例子:
class test{
public function hello(){
$this->testing(); // This is what I am using
testing(); // Does this work?
}
private function testing(){
echo 'hello';
}
}
我之所以问是因为我用在它的预定义PHP函数array_map功能,现在我打算使用由我定义的函数。这就是我的意思是:
class test{
public function hello(){
array_map('nl2br',$array); // Using predefined PHP function
array_map('mynl2br',$array); // My custom function defined within this class
}
private function mynl2br(){
echo 'hello';
}
}
为什么你不测试它是否工作? – 2012-03-14 11:59:43
只是想知道:为什么你不试试,如果它的作品?据我所知,我不会工作。你将不得不提供'array_map(array($ this,'mynl2br'),$ array);'。有关更多信息,请参见[php手册](http://php.net/manual/en/language.pseudo-types.php)。 – fresskoma 2012-03-14 12:01:11
可能的重复http://stackoverflow.com/questions/9701509/is-this-required-when-calling-local-method-inside-a-class,http://stackoverflow.com/questions/1050598/why-does -php-require-an-explicit-reference-to-this-to-call-member-functions – Yaniro 2012-03-14 12:02:44