當我從我的班級調用本地方法時,如下面的示例所示,是否必須在其之前放置$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