Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument passed to function must be callable, array given

Tags:

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.

like image 648
padarom Avatar asked Apr 02 '17 16:04

padarom


1 Answers

Change the visibility of your callback method to public.

protected function doSomething() {     $discoveries = $this->findSomething();     $discoveries->each([$this, 'doSomethingElse']); }  public function doSomethingElse($element) {     $element->bar();     // And some more } 
like image 196
BKB Avatar answered Sep 29 '22 11:09

BKB