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/07/01 17:29] – 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 => $this-> | ||
- | } | ||
- | </ | ||
- | |||
- | 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 '' | ||
- | |||
- | At this time, Ilija is still investigating whether it is more sensible to put the uninitialized check on the wrapping property, when visibility is checked, or only on the backing property, so the error would be thrown from within the hook body. In practice it will not make much difference to the developer, so we consider either case acceptable depending on what ends up being easier to do. | ||
- | |||
- | 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 111: | Line 54: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | |||
- | There will be two independent primary votes, one for each of the improvements suggested above. | ||
<doodle title=" | <doodle title=" | ||
- | * Yes | ||
- | * No | ||
- | </ | ||
- | |||
- | |||
- | <doodle title=" | ||
* Yes | * Yes | ||
* No | * No |
rfc/hook_improvements.1719854995.txt.gz · Last modified: 2024/07/01 17:29 by crell