r/PHPhelp • u/BaronOfTheVoid • Dec 09 '25
Static Function outside of classes
I know, it doesn't make any sense.
To make it clear this is about closures/callables that you can for example pass on to another function, like this: https://3v4l.org/5qegC
I have seen it in the codebase I'm working on and don't have any clue what it actually differentiates from just leaving the static keyword out, if anything. Couldn't find anything related on SO or the PHP docs either.
And I was actually expecting PHP to tell me "unexpected keyword static" or something along the lines, or if not PHP itself then at least one of the code quality tools like phpstan, cs, md and so on, or PhpStorm. But it isn't considered wrong by any of these tools.
Maybe because they didn't expect a dev could be that silly to use the static keyword outside classes.
Now I'm expecting that it at least doesn't do anything, that there is no harm in it. But maybe someone with a deeper understanding of how PHP works internally could clear that up. Thanks in advance.
View all comments
Show parent comment
1
u/LordAmras Dec 10 '25 edited Dec 10 '25
Isn't that the point that there is no difference between ?
foo(function ()and
foo(static function()Why would your case by any different because it's inside a class ? The static is not in reference to the class, since you are passing it as a parameter you are just declaring it
I'm sure there might be issues with VLD, but I would honestly tend to believe it.
Just for fun I've tried using your class (with return instead of echo) and put it into a 10m iteration loop.
This are the results on my pc (php 8.2.28):
Time taken 10,000,000: (STATIC) 5.4047768115997 seconds
Time taken 10,000,000 (NO STATIC): 5.9040310382843 seconds
---
Time taken 10,000,000: (STATIC) 5.3987510204315 seconds
Time taken 10,000,000 (NO STATIC): 5.435170173645 seconds
----
Time taken 10,000,000: (STATIC) 5.1187119483948 seconds
Time taken 10,000,000 (NO STATIC): 5.7018761634827 seconds
----
Time taken 10,000,000: (STATIC) 5.6223659515381 seconds
Time taken 10,000,000 (NO STATIC): 5.3005919456482 seconds
I don't think there seems to be any performance difference between the two.