0

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.

webhound
  • 339
  • 1
  • 4
  • 13

1 Answers1

4

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.

Blizz
  • 8,082
  • 2
  • 33
  • 53
  • Thank you! I probably could have found that in the docs somewhere, but I write PHP about once every two months. Can I assume that you can only add two members into this sort of callable array? And while I have your ear is $className in the static variety just another string? – webhound Jun 27 '18 at 21:01
  • You can click through to the callable manual page if you want for a lot of info, but yes, it's always a context and a method. As for the static calling, I added a couple examples. – Blizz Jun 27 '18 at 21:12