如何编写/引用一个PHP可调用的函数易于管理我的IDE [英] How to code/reference to a PHP callable functions easy to manage for my IDE

查看:146
本文介绍了如何编写/引用一个PHP可调用的函数易于管理我的IDE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我必须写一个可调用函数的引用时,我使用PHP的标准语法定义为


PHP函数通过其名称作为字符串传递。可以使用任何内置或用户定义的函数 [... omitted ...]



实例化对象的方法作为包含索引0处的对象和方法名(aka字符串)
在索引1处的对象的数组传递。



静态类方法也可以通过传递类名(仍然是字符串)
而不是实例化该类的对象来传递

'ClassName

从PHP 5.2.3开始,还可以传递 :: methodName'



除了常见的用户定义函数外,匿名函数也可以传递给回调参数。


所有这些方法对于函数名称refactor 查找



在我的答案中,我提出了一个解决方案,但还有其他方法可以应用,甚至完全不同,允许IDE找到方法?

解决方案

您已经是最短的事情的旁边



您可以在函数调用中直接调用匿名函数,而不使用变量



例如,您可以替换:

  $ callable = function($ param)use($ object){
return $ object-> myMethod($ param);
}
call_user_func($ callable,$ param);

由:

 code> call_user_func(function($ param)use($ object){
return $ object-> myMethod($ param);
},$ param);

您必须等待箭头函数,你应该可以使用类似的:

  call_user_func(fn($ a)=> $ object-> myMethod($ a),$ param); 


When I have to write a reference to a callable function I use the standard syntax of PHP defined as:

A PHP function is passed by its name as a string. Any built-in or user-defined function can be used [... omitted...].

A method of an instantiated object is passed as an array containing an object at index 0 and the method name (aka string) at index 1.

Static class methods can also be passed without instantiating an object of that class by passing the class name (still a string) instead of an object at index 0.

As of PHP 5.2.3, it is also possible to pass (the string) 'ClassName::methodName'.

Apart from common user-defined function, anonymous functions can also be passed to a callback parameter.

All of these ways are not "IDE friendly" for operations like function name refactor or find usage of.

In my answer I propose a solution, but there are other approaches that can be applied, even totally different, that allow to IDE to "find" the invocation of the methods?

解决方案

You already are next to the shortest thing you can do

You can perfectly call your anonymous function directly in your function call without using a variable

For instance, you can replace:

$callable=function($param) use ($object){ 
   return $object->myMethod($param); 
}
call_user_func($callable, $param);

by:

call_user_func(function($param) use ($object){ 
   return $object->myMethod($param); 
}, $param);

You will have to wait for arrow functions in future PHP versions, and you should be able to use something like:

call_user_func(fn($a) => $object->myMethod($a), $param);

这篇关于如何编写/引用一个PHP可调用的函数易于管理我的IDE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆