When I call a local method from my class as shown in the example below, do I have to put $this-> before it?
Example:
class test{
public function hello(){
$this->testing(); // This is what I am using
testing(); // Does this work?
}
private function testing(){
echo 'hello';
}
}
The reason why I ask is because I am using the array_map function with a predefined PHP function in it and I am now going to use a function defined by me. This is what I mean:
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';
}
}
Yes, it is required.
testing()refers to the global function by that name, and will cause an error if the function doesn’t exist.You can, however, make a “callback” with the
$thisvariable. As you can see from the PHP manual, you need to make an array where the first element is the object and the second element is the method name. So here you could do this: