Basically the purpose of the static keyword is totally clear to me, the PHP docs only explain the purpose of the keyword in the context of classes. I noticed one of my IDE plugins suggesting me that I should declare many of my callback functions as static.
Without static:
$myUniqueArray = unique($arrayToFilter,
function (ExamQuestion $examQuestion) {
return $examQuestion->getId();
}
);
With static:
$myUniqueArray = unique($arrayToFilter,
static function (ExamQuestion $examQuestion) {
return $examQuestion->getId();
}
);
For the result it does not make a difference, it works both. What exactly is the difference between a callback function and a static callback function in PHP under the hood? What are possible benefits and drawbacks in this context?
CodePudding user response:
You're referring to Static Anonymous Functions [DOC] which is introduced as the following in the documentation:
Anonymous functions may be declared statically. This prevents them from having the current class automatically bound to them. Objects may also not be bound to them at runtime.
If you compare that with the explanation of the static
keyword in the context of class methods [DOC], this might make the relation more clear. These are introduced as the following in the documentation:
Because static methods are callable without an instance of the object created, the pseudo-variable
$this
is not available inside methods declared as static.
So an actual difference is that you don't have $this
bound / available within the anonymous function when it is static
.
The reason why you get the suggestion within the IDE is that static anonymous functions give you a slightly better performance over the non-static variant. So unless you need $this
within the function, you can safely use the static
variant over the non-static one.
Anonymous functions have been introduced in PHP 5.3, both with and without the static
keyword [RFC] [5.3.0]. In PHP 5.3 $this
was not automatically bound when defined within a class (intentionally) and has been changed with PHP 5.4 and it is since then $this
is automatically bound for the (non-static) anonymous function.
Since PHP 7.4 you can find arrow functions [DOC] which also come in the static/non-static flavours. However:
Arrow functions support the same features as anonymous functions, except that using variables from the parent scope is always automatic.
It's not only $this
that a (non-static) arrow function would bound, it is (even for static ones) that all variables from the parent scope are automatically in use. So this will more likely hit performance than give the occasional benefit of static
for anonymous functions.
As you haven't shared which IDE, it is only a guess to which concrete suggestion you're referring to. Our educated guess is Phpstorm with the EA inspections plugin:
[EA] This closure can be declared as static (better scoping; in some cases can improve performance).
From the Static closures can be used EA inspection. And with the further information:
Analyzes closures and suggests using static closures instead.
This can bring additional performance improvements, e.g. as here:
Also, by using static function () {} closures, we squeezed out another 15% of hydration performance when dealing with private properties.
(from the inspection description provided by Php Inspections (EA Ultimate) within Phpstorm)