rfc:hook_improvements
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:hook_improvements [2024/06/29 14:19] – Make performance gains more vague crell | rfc:hook_improvements [2024/07/31 01:09] (current) – crell | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Property hook improvements ====== | ====== PHP RFC: Property hook improvements ====== | ||
* Version: 0.9 | * Version: 0.9 | ||
- | * Date: 2024-06-28 | + | * Date: 2024-06-28 |
* Author: | * Author: | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | While finalizing the implementation of the [[rfc: | + | While finalizing the implementation of the [[rfc: |
===== Proposal ===== | ===== Proposal ===== | ||
- | There are two improvements | + | There is one improvement |
- | + | ||
- | ==== Remove the proactive guard against recursive backing value access ==== | + | |
As defined in the original RFC, the following code would have been detected and throw a dedicated error. | As defined in the original RFC, the following code would have been detected and throw a dedicated error. | ||
Line 39: | Line 37: | ||
While looking for optimizations, | While looking for optimizations, | ||
- | A side effect of that optimization, | + | A side effect of that optimization, |
We consider the slightly worse error handling fully justified by the performance improvement, | We consider the slightly worse error handling fully justified by the performance improvement, | ||
- | ==== Partial support for readonly properties ==== | ||
- | |||
- | Support for hooks on '' | ||
- | |||
- | We therefore propose to allow both '' | ||
- | |||
- | The main concern is that readonly implies a property is immutable and idempotent. | ||
- | |||
- | <code php> | ||
- | class Dumb | ||
- | { | ||
- | public readonly int $value { get => random_int(1, | ||
- | } | ||
- | </ | ||
- | |||
- | On the other hand, there is no shortage of dumb things that people can do with PHP already. | ||
- | |||
- | <code php> | ||
- | readonly class ProductFromDB | ||
- | { | ||
- | public string $id; | ||
- | | ||
- | public function __construct( | ||
- | public string $name, | ||
- | public float $price, | ||
- | public Category $category, | ||
- | ) {} | ||
- | } | ||
- | |||
- | // Generated code. | ||
- | readonly class LazyProduct | ||
- | { | ||
- | // Assigned via reflection or a closure. | ||
- | private DbConnection $dbApi; | ||
- | | ||
- | // Assigned via reflection or a closure. | ||
- | private string $categoryId; | ||
- | |||
- | public Category $category { | ||
- | get { | ||
- | $this-> | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | That is, we feel, an entirely reasonable use of hooks, and would allow for lazy-load behavior per-property on readonly classes. | ||
- | |||
- | This is subtly different from the Lazy Proxy RFC, which operates on the whole object at once. We believe both use cases are valuable and should be supported. | ||
- | |||
- | A '' | ||
- | |||
- | On balance, we believe the advantages and use cases for a lazy readonly property outweigh the potential for developers to do silly things. | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 110: | Line 55: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | There will be two independent primary votes, one for each of the improvements suggested above. | + | |
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== |
rfc/hook_improvements.1719670753.txt.gz · Last modified: 2024/06/29 14:19 by crell