I'm trying to run a method on each element inside a collection. It's an object method residing in the same class:
protected function doSomething()
{
$discoveries = $this->findSomething();
$discoveries->each([$this, 'doSomethingElse']);
}
protected function doSomethingElse($element)
{
$element->bar();
// And some more
}
If I precede the call on Collection::each
with the check is_callable([$this, 'doSomethingElse'])
it returns true, so apparently it is callable. The call itself however throws an exception:
Type error: Argument 1 passed to Illuminate\Support\Collection::each() must be callable, array given, called in ---.php on line 46
The method trying to be called can be found here.
I'm bypassing this by just passing a closure that itself simply calls that function, but this would definitely a much cleaner solution and I can't find out why it throws the error.
callable
.is_callable
returns true, so I would assume it works when passed to a function expecting acallable
.doSomethingElse()
aspublic
. IMHO I suppose thateach()
is called in the context of thecollection
class that does not have visibility on protected functions in other classes.findSomething
ordoSomethingElse
is throwing an exception. In my limited experience, this error message is a red herring.