Take the State of PHP 2026 Survey →

Property hook improvements View as Markdown

Implemented (PHP 8.4)
Authors
Ilija Tovilo (tovilo.ilija@gmail.com), Larry Garfield (larry@garfieldtech.com)
Date
2024-06-28
Version
0.9
Source
wiki.php.net/rfc/hook_improvements

Vote

Remove the extra hook recursion guard? closed
Yes
33
No
0
33 voters
alcaeus Yes 2024-07-16 06:28
beberlei Yes 2024-07-15 19:26
bwoebi Yes 2024-07-15 19:31
crell Yes 2024-07-15 19:03
derick Yes 2024-07-21 23:55
devnexen Yes 2024-07-16 05:24
ericmann Yes 2024-07-15 19:21
galvao Yes 2024-07-19 12:58
girgias Yes 2024-07-15 22:07
ilutov Yes 2024-07-15 19:06
jhdxr Yes 2024-07-26 14:55
jimw Yes 2024-07-15 19:08
josh Yes 2024-07-25 16:33
kalle Yes 2024-07-16 01:42
kguest Yes 2024-07-17 10:42
kocsismate Yes 2024-07-25 20:04
lbarnaud Yes 2024-07-15 19:24
mauricio Yes 2024-07-24 18:02
mbeccati Yes 2024-07-19 07:22
nicolasgrekas Yes 2024-07-17 06:32
nielsdos Yes 2024-07-15 19:07
patrickallaert Yes 2024-07-25 14:56
petk Yes 2024-07-15 20:55
pierrick Yes 2024-07-16 13:22
ramsey Yes 2024-07-16 03:31
rasmus Yes 2024-07-17 00:50
reywob Yes 2024-07-30 08:59
sebastian Yes 2024-07-16 06:29
sergey Yes 2024-07-15 19:16
theodorejb Yes 2024-07-16 14:04
timwolla Yes 2024-07-15 19:05
trowski Yes 2024-07-16 16:24
weierophinney Yes 2024-07-23 13:08

Discussions on the mailing list

[RFC] Property Hook improvements
by Larry Garfield · 17 messages
[Vote] Property Hook performance improvements
by Larry Garfield · 2 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC

Introduction

While finalizing the implementation of the property hooks RFC patch, we have found a small area for improvement. This is nominally changes from the behavior defined in the original RFC, and thus we want to refer them through the RFC process.

Proposal

There is one improvement we propose.

As defined in the original RFC, the following code would have been detected and throw a dedicated error.

class C
{
    public string $val = 'a' {
        get {
          return $this->val . $this->suffix();
        }
    }
 
    public function suffix(): string
    {
        return $this->val;
    }
}
 
$c = new C();
print $c->val;

While looking for optimizations, Ilija found he was able to refactor the hook logic to make it more than twice as fast: While there is still some overhead to a hook compared to a traditional get method, it's been reduced from “close to the cost of __get” to “almost negligible compared to a method.” Enough that it would almost certainly be swamped by the cost of the hook body itself. This is a massive improvement.

A side effect of that optimization, however, is that we cannot proactively detect the bug above. Instead, it would result in an infinite loop, which would eventually trigger a stack limit exception from PHP. (That logic already exists.)

We consider the slightly worse error handling fully justified by the performance improvement, and by the fact that the code above is a bug no matter what (it's just a question of when the bug gets reported). But as it is a change from the behavior stated in the previous RFC, we are putting it out for consideration.

Backward Incompatible Changes

None.

Proposed PHP Version(s)

PHP 8.4.

RFC Impact

Proposed Voting Choices

Vote: Remove the extra hook recursion guard? — Yes: 33 · No: 0 (closed)

Patches and Tests

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.