是否有数组可调用函数?(Is there an array

编程入门 行业动态 更新时间:2024-10-25 15:25:31
是否有数组可调用函数?(Is there an array-to-callable function?)

设置

在SomeClass有f2member获取两个整数参数并产生其总和。 测试通过显示调用实际工作并检索预期结果。 这是用两个参数1和1调用$g返回2 。

重要说明:这仅适用于php 5.4.11及向上 兼容性检查

class SomeClass extends PHPUnit_Framework_TestCase { function f2member($a,$b) { return $a + $b; } /** * @test */ public function test() { $g = array($this,'f2member'); $this->assertEquals(2, $g(1,1)); // squiggle line under $g } }

问题

但是,这会在每个方法调用的phpStorm内部产生一个警告,并在$g下产生一个波浪线:

函数名必须是可调用的 - 字符串,Closure或实现__invoke的类,当前是数组

警告的起源对我来说很清楚,现在我正在寻找避免这些警告的方法。 一个要求是,我不想改变调用函数的样式。 我不想做的另一件事是停用此警告。 我宁愿在它周围包装一些东西,它为类型系统提供了必要的信息。

尝试

我已经遇到了几个删除警告的解决方案。 一种是定义用户定义的函数,该函数仅记录所需的目标类型。

/** * @param array $arr * * @return callable */ function callable_for($arr) { return $arr; }

这会返回一个数组,但也会明确地告诉类型系统来自callable_for函数的内容。 有了这种类型的注释,phpStorm现在停止抱怨这个警告,虽然它仍然返回一个数组。

$g = callable_for(array($this,'f2member'));

是不是像我在php中的callable_for一样开箱即可实现这一目标? 如果答案是否定的,那么我正在寻找我们能找到的最简洁的解决方案。

我已经尝试过搜索SO, php.net和谷歌。 也许,我只是搜索了错误的单词组合,这里只是两个样本:

数组到可调用的php 创建可调用方法处理php

BigPicture

以防嫌疑人出现这是一个X / Y问题 :我有另一个函数将可调用作为参数。 使用闭包,定义一些东西是很自然的,可以在以后调用。 但是,如何为成员或静态方法定义可调用而不将其包装在另一个委托Closure中? 数组表示法允许用于统一传递:闭包或静态/成员方法句柄到我以后的函数。 我现在正试图找到一个简洁的解决方案,接近这一点 。

Setting

Within SomeClass there is f2member taking two integer arguments and producing its sum. The test passes showing that the call actualy works and retrieves the expected result. Which is calling $gwith two parameters 1 and 1 returning 2.

Important: This works only for php 5.4.11 and upwards compatibility check

class SomeClass extends PHPUnit_Framework_TestCase { function f2member($a,$b) { return $a + $b; } /** * @test */ public function test() { $g = array($this,'f2member'); $this->assertEquals(2, $g(1,1)); // squiggle line under $g } }

Problem

However, this produces a warning inside phpStorm on every method invocation and a squiggle line under $g:

Function name must be callable - string, Closure or class implementing __invoke, currently array

The origin of the warning is clear to me and now i am looking for ways to avoid these warnings. A requirement is, that i dont want to change the style of calling the function. Another thing i don't want to do is to deactivate this warning. I would rather prefer to wrap something around it, which provides the necessary information to the type system.

Attempt

I already encountered several solutions to remove the warnings. One is to define a user defined function, which only documents the required target type.

/** * @param array $arr * * @return callable */ function callable_for($arr) { return $arr; }

This returns an array, but also explicitly tells the type system what comes out of the callable_for function. With this type annotation in place phpStorm now stops complaining about this warning, although it still returns an array.

$g = callable_for(array($this,'f2member'));

Question

Isn't there something out of the box like my callable_for in php to achieve this? If the answer is no, then i am looking for the most concise solution we can find.

I already tried looking on SO, php.net and google. Maybe, I just searched for the wrong word combinations, here are just two samples:

array to callable php create callable method handle php

BigPicture

Just in case suspects arise this is a X/Y problem: I have another function taking a callable as a parameter. With closures it is very natural to define something, which can be invoked later on. However, how do i define a callable for a member or a static method without wrapping it in another delegation Closure? The array notation allows to be used to uniformly pass: closures or static/member method handles to my later function. I am now trying to find a concise solution to this, which comes close to this.

最满意答案

因此,另一个进步可能是,修改callable_for以获取两个参数并包装两个职责; 创建数组并记录目标返回类型。

/** * @param mixed $context * @param string $method * * @return callable */ function callable_for($context, $method) { return array($context, $method); }

使用此实现将$g声明的简洁性提高到可接受的水平。

$g = callable_for($this,'f2member');

此函数仍返回一个数组,但类型系统可以使用给定的信息来正确处理此数组以进行动态方法调用。

Thus, another advancement could be, to modify callable_for to take two arguments and wrap both responsibilities; to create the array and to document the target return type.

/** * @param mixed $context * @param string $method * * @return callable */ function callable_for($context, $method) { return array($context, $method); }

Using this implementation raises the conciseness of $g's declaration to an acceptable level.

$g = callable_for($this,'f2member');

This function still returns an array, but the type system can use the given information to correctly treat this array for dynamic method invocation.

更多推荐

本文发布于:2023-07-05 01:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   函数   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!