How can array_map function be used with class methods in PHP?
When using the array_map function with class methods in PHP, you need to pass the class instance as the first parameter and the method name as the second parameter. This allows you to apply the method to each element of the array.
class MyClass {
public function myMethod($value) {
return $value * 2;
}
}
$myClass = new MyClass();
$array = [1, 2, 3, 4, 5];
$result = array_map([$myClass, 'myMethod'], $array);
print_r($result);