laravel 传递给函数的参数必须是可调用的,给定数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43170785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Argument passed to function must be callable, array given
提问by Padarom
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:
如果我在调用之前进行Collection::each
检查,is_callable([$this, 'doSomethingElse'])
它将返回 true,因此显然它是可调用的。然而,调用本身会引发异常:
Type error: Argument 1 passed to Illuminate\Support\Collection::each() must be callable, array given, called in ---.php on line 46
类型错误:传递给 Illuminate\Support\Collection::each() 的参数 1 必须是可调用的,给定的数组,在第 46 行的 ---.php 中调用
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.
我只是通过传递一个本身只是调用该函数的闭包来绕过它,但这绝对是一个更清晰的解决方案,我无法找出它抛出错误的原因。
回答by BKB
Change the visibility of your callback method to public.
将回调方法的可见性更改为 public。
protected function doSomething()
{
$discoveries = $this->findSomething();
$discoveries->each([$this, 'doSomethingElse']);
}
public function doSomethingElse($element)
{
$element->bar();
// And some more
}
回答by Shadrix
Since PHP 7.1 you can leave your function protected. Now you can write:
自 PHP 7.1 起,您可以保护您的函数。现在你可以写:
protected function doSomething()
{
$discoveries = $this->findSomething();
$discoveries->each(\Closure::fromCallable([$this, 'doSomethingElse']));
}
protected function doSomethingElse($element)
{
$element->bar();
// And some more
}
回答by GreeKatrina
PHP >= 5.4
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
而不是使用$this
,如下所示:
$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.
尽管$discoveries
和$this
属于同一类,因此可以访问彼此的受保护和私有方法,但类型提示功能可能不会检查回调数组中的对象是否与当前类是同一类。但是,该is_callable()
方法会对此进行检查,这可以解释为什么从each()
方法内部调用它时它返回 true 。
PHP < 5.4
PHP < 5.4
There is no type named callable
, so when you use it as a type hint, it is referring to a classnamed callable
. See this answer.
没有名为 的类型callable
,因此当您将其用作类型提示时,它指的是名为的类callable
。看到这个答案。