29

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.

padarom
  • 3,529
  • 5
  • 33
  • 57
  • 2
    @bxN5 It's not supposed to be a function, it's supposed to be a `callable`. `is_callable` returns true, so I would assume it works when passed to a function expecting a `callable`. – padarom Apr 02 '17 at 16:44
  • 3
    please try to declare `doSomethingElse()` as `public`. IMHO I suppose that `each()` is called in the context of the `collection` class that does not have visibility on protected functions in other classes. – dparoli Apr 02 '17 at 21:32
  • I suspect that `findSomething` or `doSomethingElse` is throwing an exception. In my limited experience, this error message is a red herring. – Michael Easter Aug 18 '17 at 21:00
  • That might be the case, though in the meantime I solved it differently and can't remember the exact place where I used it, thus I'm unable to verify. – padarom Aug 18 '17 at 23:08

4 Answers4

47

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
}
BKB
  • 635
  • 5
  • 4
28

Since PHP 7.1 you can leave your function protected. Now you can write:

protected function doSomething()
{
    $discoveries = $this->findSomething();
    $discoveries->each(\Closure::fromCallable([$this, 'doSomethingElse']));
}

protected function doSomethingElse($element)
{
    $element->bar();
    // And some more
}

Source

Philipp Mochine
  • 4,351
  • 10
  • 36
  • 68
3

function with callback

public function foo(callable $callback){ 
    $callback('something') 
} 

named function for callback

public function do($something) { 
   // 
} 

result

foo(function ($something) { 
        // 
})

or call named function

foo(\Closure::fromCallable([$this, 'do']))

or

foo(this->do(...))
programm011
  • 141
  • 1
  • 1
  • 6
-1

PHP >= 5.4

I wasn't able to reproduce your error, but my guess is that you should use $discoveries instead of $this in the callback array, like so:

$discoveries->each([$discoveries, 'doSomethingElse']);

Even though $discoveries and $this are of the same class, and therefore can access each other's protected and private methods, the type-hinting functionality may not check that the object in the callback array is the same class as the current class. However, the is_callable() method will check for this, which may explain why it returns true when you call it from inside the each() method.

PHP < 5.4

There is no type named callable, so when you use it as a type hint, it is referring to a class named callable. See this answer.

Katrina
  • 1,922
  • 2
  • 24
  • 42
  • 1
    I didn‘t specify the Laravel version I was using, but it requires PHP 5.6+, so the <5.4 case is a non-issue. I‘ll give the other case a try however – padarom Dec 05 '17 at 20:32