How can you access an array_map function in an object in PHP?
To access an array_map function in an object in PHP, you can define a custom method within the object that utilizes the array_map function to map a callback function to each element of an array property in the object. This allows you to apply a transformation to each element of the array without directly accessing the array outside of the object.
class CustomObject {
private $data = [1, 2, 3, 4, 5];
public function mapData($callback) {
$this->data = array_map($callback, $this->data);
}
}
$obj = new CustomObject();
$obj->mapData(function($value) {
return $value * 2;
});
print_r($obj);