rfc:mixed_vs_untyped_properties

This is an old revision of the document!


PHP RFC: Harmonise "untyped" and "typed" properties

Introduction

This RFC proposes to remove the distinction between “typed” and “untyped” properties, by treating any property with no type information as though it was declared mixed. This is primarily aimed to reduce confusion around different states, error messages, and behaviours; to do so, it makes the language stricter in some circumstances.

PHP currently has three primary ways of adding properties to an object:

  1. Dynamically. The property is created automatically on a specific instance when it is first assigned to, and can be deleted completely using unset.
  2. Declared, with optional visibility. The property is part of the class definition, and allocated on every instance, even if it is never assigned. The behaviour of unset is complex, hiding but not fully deleting the property.
  3. Declared with a type. In addition to being allocated on every instance, the property is covered by extra guards on assignment to guarantee its type. If it is never assigned a value, or passed to unset, it is assigned a special “uninitialized” state.

The different behaviours of these properties are largely a result of the history of the language, rather than a consistent design. In particular, with the addition of the ''mixed'' type, it would seem logical for private $foo; to be short-hand for private mixed $foo;, since no type-guards are needed; but this is not currently the case, due to the different handling of initial states and unset.

Current Behaviour

The three types of property vary in their initial state, and in their state after calling unset, as can be seen in these three demos, which run the same code with the three types of property: dynamic property, untyped property, typed property.

The states can be summarised in this table:

Property Declaration Initial state After assignment After unset After re-assignment
#[AllowDynamicProperties] Undefined Defined, public Undefined Defined, public
private $foo; null Defined, private ? Defined, private
private mixed $foo; Uninitialized Defined, private Uninitialized Defined, private


Where:

  • “Defined” is straight-forward: the property exists, and can be read subject to visibility constraints.
  • “Undefined” means the property does not exist on the object. It does not show up in views such as var_dump. Attempting to read it currently produces a Warning, but will produce an Error in PHP 9.0.
  • “Uninitialized” is a special state introduced as part of the introduction of typed properties to handle cases where neither null nor an inline initializer can be used. The property is still treated as present on the instance, but with a special value/state; in the example, the var_dump output shows this as [“foo”:“A”:private] => uninitialized(mixed)

The state marked “?”, for untyped properties after unset, is a complex one:

  • In output such as var_dump, it is not listed, as with “undefined”
  • *Reading* it gives a Warning (and future Error) of “Undefined property”
  • Reading or writing still obeys the original visibility constraint - reading an unset private property from outside the class gives “Cannot access private property”, not “Undefined property”
  • Writing to it does not give the deprecation notice for “Creation of dynamic property”; instead, the original declaration (including any visibility specifier) is silently re-used

Dynamic properties will be prohibited on most classes in 9.0, giving the following if we don't make other changes:

var_dump output Error on read Error on write
True undefined Not shown “Undefined property” “Creation of dynamic property”
(unless on stdClass or with #[AllowDynamicProperties])
Declared then unset Not shown “Undefined property” None
Typed and uninitialized uninitialized(mixed) “must not be accessed before initialization” None

Proposal

Backward Incompatible Changes

Un-typed properties will no longer have an implicit initial value of null.

Proposed PHP Version(s)

'TODO' List the proposed PHP versions that the feature will be included in. Use relative versions such as “next PHP 8.x” or “next PHP 8.x.y”.

RFC Impact

To SAPIs

'TODO' Describe the impact to CLI, Development web server, embedded PHP etc.

To Existing Extensions

'TODO' Will existing extensions be affected?

To Opcache

'TODO' 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.

Open Issues

'TODO' Make sure there are no open issues when the vote starts!

Unaffected PHP Functionality

'TODO' 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

'TODO' This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.

Proposed Voting Choices

'TODO' 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

  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

Rejected Features

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

rfc/mixed_vs_untyped_properties.1700149813.txt.gz · Last modified: 2023/11/16 15:50 by imsop