rfc:property_type_hints

This is an old revision of the document!


PHP RFC: Property type-hints

Introduction

With the addition of type-checked function return values in PHP 7, we are one step closer to PHP being a gradually-typed language. This RFC proposes to close that gap with the addition of optional type-checked properties, by allowing the use of static type-hints for properties.

Given that objects expose two types of public members, methods and properties, with optional type-checking for function arguments already present, and with the addition of type-checking for function return-values in PHP 7, the addition of type-checking for public properties is natural, and makes OOP in PHP fully gradually-typed.

The significance of type-checked properties is a given, as demonstrated by it's inclusion in Hack, as well as other recent scripting languages, including Typescript, Dart and ActionScript. The need for type-hinting is demonstrated by the widespread use of php-doc, and support for such type-hinting in modern PHP IDEs.

The proposed syntax is compatible with that of Hack, and is a natural addition to the language, resembling the syntax set forth by plenty other gruadually-typed (and statically-typed) languages.

Proposal

The only proposed change to the syntax, is the addition of an optional type-hint in property declarations - for example:

class BoxedInt {
    public int $value;
}

This declares a public property $value which can only be initialized or set with an integer scalar value.

Scalar type hints, as per the addition in PHP 7, are permitted, as well as any class or interface name.

Violating the type-check when initializing or setting a property, will result in a catchable fatal error:

$i = new BoxedInt;
$i->value = 'oops';
// Catchable fatal error: property BoxedInt::$value must be of the type integer, string given

Consistent with return type-check violations, and scalar type-hinting, this example will generate an E_RECOVERABLE_ERROR.

Backward Incompatible Changes

None.

Proposed PHP Version(s)

Next PHP 7.x.

RFC Impact

To Opcache

The impact on opcache needs to be examined.

Open Issues

To be determined: documentation for existing, standard classes (reflection, spl, etc.) may need updates, e.g. adding type-hints to existing class documentation.

Unaffected PHP Functionality

The introduction of an optional type-hint does not affect legacy PHP code.

Future Scope

A future version of this proposal might include the addition of a new pseudo-type (“any” or “mixed”) which would allow developers to indicate that they have thought about the type-safety of a given property; having no type-hint could indicate that you simply didn't think about it. (Currently, developers may deal with this issue by explicitly type-hinting as “mixed” using php-doc.)

Proposed Voting Choices

TBD

Patches and Tests

This RFC needs a volunteer to help with prototyping and implementation.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

TBD

Rejected Features

TBD

rfc/property_type_hints.1437306566.txt.gz · Last modified: 2017/09/22 13:28 (external edit)