logoalt Hacker News

hyperionultrayesterday at 12:32 PM2 repliesview on HN

~3% performance gains. I didn’t understood part about $this.


Replies

rererereferredyesterday at 1:11 PM

If the closure doesn't use $this (an instance of the current class) then it doesn't need to store a reference to it, which also skips the bookkeeping from the garbage collector.

ethan_smithyesterday at 6:52 PM

In PHP, closures defined inside a class method automatically capture `$this`, which means the closure holds a reference to the entire object even if it never uses it. This prevents the object from being garbage collected and adds overhead. The optimization detects when `$this` isn't actually used and makes the closure static automatically, dropping that unnecessary reference.