Reputation: 28284
I was reading through the code and came across this syntax:
$customerIDs = array_map(function(Customer $customer) { return $customer->id; }, $customers);
where $customers
is the array.
My confusion is in trying to understand function(Customer $customer)
. I see that Customer
is a class, but what is $customer
then?
Upvotes: 0
Views: 81
Reputation: 224903
This part:
function(Customer $customer) { return $customer->id; }
is an anonymous function. It's a "new" feature in PHP 5.3. It's pretty much equivalent to:
function someFunction(Customer $customer) {
return $customer->id;
}
$customerIDs = array_map('someFunction', $customers);
As for the Customer $customer
part, that's just a type-constrained argument. It throws an error if the argument passed is not of type Customer
.
You can read more about anonymous functions at the php.net documentation.
Upvotes: 4