rfc:property-capture
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:property-capture [2023/04/22 21:16] – imsop | rfc:property-capture [2023/04/23 21:15] (current) – imsop | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Property Capture for Anonymous Classes ====== | ====== PHP RFC: Property Capture for Anonymous Classes ====== | ||
- | * Version: 0.2 | + | * Version: 0.3 |
- | * Date: 2023-04-22 | + | * Date: 2023-04-23 |
* Author: Rowan Tommins (imsop@php.net) | * Author: Rowan Tommins (imsop@php.net) | ||
* Thanks: Nicolas Grekas (nicolasgrekas@php.net), | * Thanks: Nicolas Grekas (nicolasgrekas@php.net), | ||
Line 35: | Line 35: | ||
===== Syntax ===== | ===== Syntax ===== | ||
- | A new //'' | + | A new //'' |
==== Basic Form ==== | ==== Basic Form ==== | ||
Line 155: | Line 155: | ||
===== Examples ===== | ===== Examples ===== | ||
- | **TODO** | + | **TODO |
+ | |||
+ | Create a struct-like object with readonly public properties: | ||
+ | |||
+ | <code php> | ||
+ | $id = get_next_id(); | ||
+ | $name = get_name(); | ||
+ | $user = new readonly class use ($id, $name) {}; | ||
+ | echo " | ||
+ | $user-> | ||
+ | </ | ||
+ | |||
+ | Decorate a [[https:// | ||
+ | |||
+ | <code php> | ||
+ | use Psr\Log\{LoggerInterface, | ||
+ | |||
+ | function decorate_logger(LoggerInterface $logger, string $contextKey, | ||
+ | | ||
+ | use ($logger as private $innerLogger, | ||
+ | implements LoggerInterface | ||
+ | { | ||
+ | public function log($level, string|\Stringable $message, array $context = []): void { | ||
+ | $context[$this-> | ||
+ | $this-> | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | </ | ||
===== Reflection ===== | ===== Reflection ===== | ||
Line 170: | Line 198: | ||
===== Restrictions ===== | ===== Restrictions ===== | ||
- | Because it generates both property declarations and a constructor, | + | The following new errors follow from the use of properties, rather than a new mechanism, to access |
* " | * " | ||
* " | * " | ||
+ | |||
+ | The following new errors follow from the current implementation' | ||
+ | |||
* " | * " | ||
* " | * " | ||
- | ==== Workarounds | + | Various alternatives to this restriction exist, discussed below. Throwing an Error now does not rule out any of these alternatives being implemented in future versions. |
+ | |||
+ | ==== Current Workaround | ||
The restrictions on custom constructors can be worked around by adding a normal instance method, and calling it immediately. | The restrictions on custom constructors can be worked around by adding a normal instance method, and calling it immediately. | ||
Line 229: | Line 262: | ||
==== Alternative 2: Automatically Calling Parent Constructor ==== | ==== Alternative 2: Automatically Calling Parent Constructor ==== | ||
- | Another | + | Another |
<code php> | <code php> | ||
Line 302: | Line 335: | ||
The main complexity here is generating the additional method call - in the above example, it is shown as called on the local variable '' | The main complexity here is generating the additional method call - in the above example, it is shown as called on the local variable '' | ||
+ | |||
+ | ==== Alternative 3b: Initialising Before the Constructor Call ==== | ||
+ | |||
+ | A variation on the above would be to inject the extra method call (or the assignments themselves) immediately //before// the constructor is called. | ||
+ | |||
+ | Although it would give slightly nicer semantics, this would likely be even more challenging to implement, since the object creation and constructor call are both part of the '' | ||
==== Alternative 4: Calling an Additional Magic Method ==== | ==== Alternative 4: Calling an Additional Magic Method ==== | ||
Line 348: | Line 387: | ||
==== Arbitrary Expressions ==== | ==== Arbitrary Expressions ==== | ||
- | **TODO** | + | When a renamed property is indicated with the '' |
+ | |||
+ | <code php> | ||
+ | $anon = new class use (self::ID as $id, get_some_value() | ||
+ | </ | ||
+ | |||
+ | Which would be equivalent to this: | ||
+ | |||
+ | <code php> | ||
+ | $anon = new class(self:: | ||
+ | public function __construct( | ||
+ | public $id, | ||
+ | private $something | ||
+ | ) {} | ||
+ | } | ||
+ | </ | ||
==== Extension to Anonymous Functions ==== | ==== Extension to Anonymous Functions ==== | ||
- | **TODO** | + | Taking the above a step further, the '' |
+ | |||
+ | <code php> | ||
+ | $callback = function() use (self::ID as $id, get_some_value() | ||
+ | do_something($id, | ||
+ | }; | ||
+ | </ | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
Line 360: | Line 420: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | **TODO** | + | https:// |
===== Implementation ===== | ===== Implementation ===== |
rfc/property-capture.1682198180.txt.gz · Last modified: 2023/04/22 21:16 by imsop