31

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.

4
  • 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
    Commented Apr 2, 2017 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
    Commented Apr 2, 2017 at 21:32
  • I suspect that findSomething or doSomethingElse is throwing an exception. In my limited experience, this error message is a red herring. Commented Aug 18, 2017 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
    Commented Aug 18, 2017 at 23:08

4 Answers 4

50

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
}
0
29

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

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(...))
-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.

1
  • 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
    Commented Dec 5, 2017 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.