dongnai5905 2018-06-27 19:52
浏览 8
已采纳

有人可以解释call_user_func_array(array(arg1,arg2),[])是如何工作的吗?

I know this is a really vague question, but I just don't intuitively get it. I'm mostly a javascript/java guy, so you'll have to excuse me.

What happens inside of that first array call when doing something like:

call_user_func_array(array($this, $this->statementType), array($args))

that turns it into a callback function that is a member of the $this object? I get that it works, I just don't fully get why and it's bugging me.

  • 写回答

1条回答 默认 最新

  • duanmi1900 2018-06-27 19:58
    关注

    In PHP you can define a callable as an array that is a combination of a calling context and a function name.

    If you use [$this, $string] it will call the function whose name is stored in $string on the $this instance. ([$this, 'functionName'] would also work, it does not need to be a variable)

    If you use [$className, $string] it will result in a static call. Examples here could be ['\Namespace\MyClass', 'functionName'] or [\NameSpace\MyClass::class, 'functionName'] or with variables. The ::class syntax has a number of advantages, as was already answered here. Static calling can also be done by a single string ("\Namespace\MyClass::functionName" for example)

    The second array is, as the function documents it, the list of parameters to pass to the function when calling it, but I assume that part was already clear.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?