This is an old revision of the document!
PHP RFC: Your Title Here
- Version: 0.1
- Date: 2023-04-15
- Author: Rowan Tommins (imsop@php.net)
- Thanks: Nicolas Grekas (nicolasgrekas@php.net), Ilija Tovilo (tovilo.ilija@gmail.com)
- Status: Draft
- First Published at: http://wiki.php.net/rfc/property-capture
Introduction
Anonymous classes were introduced in PHP 7.0, using a syntax that allows declaring a class definition and constructing an instance in a single statement. However, unlike anonymous functions, it is not easy to “capture” values from the parent scope for use inside the class; they can only be passed in via an explicit constructor or other method.
This RFC proposes a “property capture” syntax, where a property can be declared and initialised with $instance = new class use($captured) {};
Optional access modifiers, type, and renaming are also supported, e.g. $instance = new class use($localName as private readonly int $propertyName) {};
Syntax
A new “use clause” is introduced, immediately after the keywords “new class”, with the syntax use (<var-name> as <modifiers> <type> <property-name>, ...)
. The <modifiers>
, <type>
, and <property-name>
are all optional; if none is specified, the “as” keyword must be omitted.
Basic Form
The simplest form of property capture resembles the capture list of anonymous functions:
$foo = 1; $bar = 2; $anon = new class use ($foo, $bar) {};
This declares a class with public untyped properties $foo
and $bar
, and creates an instance populating them from the outer variables $foo
and $bar
. In other words, it is equivalent to this:
$foo = 1; $bar = 2; $anon = new class($foo, $bar) { var $foo; var $bar; public function __construct($foo, $bar) { $this->foo = $foo; $this->bar = $bar; } };
Renaming
By default, the property takes the same name as the outer variable, but this can be over-ridden using the syntax $varName as $propName
. This also allows the same local variable to be captured as the initial value for more than one property. For example:
$foo = 1; $bar = 2; $anon = new class use ($foo as $one, $bar as $two, $bar as $three) {};
Is equivalent to:
$foo = 1; $bar = 2; $anon = new class($foo, $bar, $bar) { var $one; var $two; vat $three; public function __construct($one, $two, $three) { $this->one = $one; $this->two = $two; $this->three = $three; } };
Modifiers and Type
The as
keyword can also be used to modify the visibility and/or type of the declared property, either instead of or as well as renaming the property.
The modifiers allowed are the same as for Constructor Property Promotion, which is used internally to declare the properties; that is currently:
- One of
public
,protected
, orprivate
- Optional
readonly
, which must be combined with a type specification - A type specification
For example:
$foo = 1; $bar = 2; $anon = new class use ($foo as private, $bar as protected readonly int, $bar as ?int $alsoBar) {};
Is equivalent to:
$foo = 1; $bar = 2; $anon = new class($foo, $bar, $bar) { private $foo; protected readonly int $bar; var ?int $alsoBar; public function __construct($foo, $bar, $alsoBar) { $this->foo = $foo; $this->bar = $bar; $this->alsoBar = $alsoBar; } };
Restrictions and Errors
Reflection
Backward Incompatible Changes
What breaks, and what is the justification for it?
Proposed PHP Version(s)
Next PHP 8.x (hopefully 8.3)
RFC Impact
To SAPIs
Describe the impact to CLI, Development web server, embedded PHP etc.
To Existing Extensions
Will existing extensions be affected?
To Opcache
It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP.
Please explain how you have verified your RFC's compatibility with opcache.
New Constants
Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation.
php.ini Defaults
If there are any php.ini settings then list:
- hardcoded default values
- php.ini-development values
- php.ini-production values
Open Issues
Make sure there are no open issues when the vote starts!
Unaffected PHP Functionality
List existing areas/features of PHP that will not be changed by the RFC.
This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise.
Future Scope
Constructor Support
Arbitrary Expressions
Proposed Voting Choices
Include these so readers know where you are heading and can discuss the proposed voting options.
Patches and Tests
Links to any external patches and tests go here.
If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.
Make it clear if the patch is intended to be the final patch, or is just a prototype.
For changes affecting the core language, you should also provide a patch for the language specification.
Implementation
After the project is implemented, this section should contain
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
- 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.