ajf

Andrea Faulds

Contents

PHP RFC: Scalar Type Hints

  • Version: 0.3.1
  • Date: 2014-12-14 (initial draft; put Under Discussion 2014-12-31; version 0.2 created 2015-01-13; withdrawn 2015-02-15)
  • Author: Andrea Faulds, ajf@ajf.me
  • Status: Withdrawn

Summary

This RFC proposes the addition of four type hints for scalar types: int, float, string and bool. These type hints would have “weak” type-checking by default, following the same casting rules traditionally used for the parameters of extension and built-in PHP functions.

This RFC further proposes the addition of a new optional per-file directive, declare(strict_types=1), which makes all function calls and return statements within a file have “strict” type-checking for scalar type hints, including for extension and built-in PHP functions. In addition, calls to extension and built-in PHP functions with this directive produce an E_RECOVERABLE_ERROR on parameter parsing failure, bringing them into line with userland type hints.

With these two features, it is hoped that more correct and self-documenting PHP programs can be written.

Example

Let's say we have a PHP class that represents an ElePHPant. We put scalar type hints on our constructor arguments:

ElePHPant.php
<?php
class ElePHPant {
    public $name, $age, $cuteness, $evil;
    public function __construct(string $name, int $age, float $cuteness, bool $evil) {
        $this->name = $name;
        $this->age = $age;
        $this->cuteness = $cuteness;
        $this->evil = $evil;
    }
}

In a separate file we might try to make a new instance like so:

main.php
<?php
require "ElePHPant.php";
 
$sara = new ElePHPant("Sara", 7, 0.99, FALSE);
var_dump($sara); /*
object(ElePHPant)#1 (4) {
  ["name"]=>
  string(4) "Sara"
  ["age"]=>
  int(7)
  ["cuteness"]=>
  float(0.99)
  ["evil"]=>
  bool(false)
} */

This call succeeds, because the types of the arguments passed exactly match the type hints.

By default, weak type hints that permit some conversions are used, so we could also pass values that are convertible and they'll be converted, just like with extension and built-in PHP functions:

main2.php
<?php
require "ElePHPant.php";
 
$nelly = new ElePHPant(12345, "7 years", "0.9", "1");
var_dump($nelly); /*
object(ElePHPant)#2 (4) {
  ["name"]=>
  string(5) "12345"
  ["age"]=>
  int(7)
  ["cuteness"]=>
  float(0.9)
  ["evil"]=>
  bool(true)
}
Notice: A non well formed numeric value encountered
*/

However, it is also possible to turn on strict type checking with an optional directive. In this mode, the same call would fail:

main3.php
<?php
require "ElePHPant.php";
 
declare(strict_types=1);
 
$nelly = new ElePHPant(12345, "7 years", "0.9", "1");
// Catchable fatal error: Argument 1 passed to ElePHPant::__construct() must be of the type string, integer given

The directive affects all function calls in the file (or declare() block if specified), regardless of whether the functions being called were declared in files which used strict type checking. So:

strict_mix.php
<?php
require "ElePHPant.php";
 
// implicitly weakly type-checked code (default)
$nelly = new ElePHPant(12345, "7 years", "0.9", "1"); // succeeds
 
declare(strict_types=1) {
    // explicitly strictly type-checked code
 
    $nelly = new ElePHPant(12345, "7 years", "0.9", "1");
    // Catchable fatal error: Argument 1 passed to ElePHPant::__construct() must be of the type string, integer given
}

This applies equally to nested function calls, which also use the strictness setting of the file:

strict_mix2.php
<?php
require "ElePHPant.php";
 
// implicitly weakly type-checked code (default)
function makeEllie() {
    return new ElePHPant(42, "19", "0.7", 1); // will succeed, no matter where makeEllie() is called from
}
 
makeEllie(); // no error
 
declare(strict_types=1) {
    // explicitly strictly type-checked code
 
    makeEllie(); // no error
 
    function makeEllie_strict() {
        return new ElePHPant(42, "19", "0.7", 1); // will fail, no matter where makeEllie_strict() is called from
    }
 
    makeEllie_strict(); // error
}
 
// implicitly weakly type-checked code, again
 
makeEllie_strict(); // error

In addition to userland functions, the strict type checking mode also affects extension and built-in PHP functions:

main4.php
<?php
 
declare(strict_types=1);
 
$foo = sin(1);
// Catchable fatal error: sin() expects parameter 1 to be float, integer given

Scalar type hints would also work for return values, as does strict type checking mode:

returns.php
<?php
 
function foobar(): int {
    return 1.0;
}
 
var_dump(foobar()); // int(1)
 
declare(strict_types=1) {
    function foobar2(): int {
        return 1.0;
    }
}
 
var_dump(foobar2());
// Catchable fatal error: Return value of foobar() must be of the type integer, float returned

However, there is a key difference between parameter and return type hints. The type-checking mode used for parameters is the one used by the file containing the function call, while the type-checking mode used for return values is the one used by the file containing the return statement (i.e. the file defining the function). So:

returns.php
<?php
 
declare(strict_types=1) {
    function foobar3(): int {
        return 1.0; // error, regardless of where it is called from
    }
 
    foobar3(); // error
}
 
foobar3(); // also error

Background and Rationale

History

PHP has had parameter type hints for interface and class names since PHP 5.0, arrays since PHP 5.1 and callables since PHP 5.4. These type hints allow the PHP runtime to ensure that correctly-typed arguments are passed to functions, and make function signatures more informative. Unfortunately, PHP's scalar types haven't been hintable.

There have been some previous attempts at adding scalar type hints, such as the Scalar Type Hints with Casts RFC. From what I can see, that specific RFC failed primarily for three reasons:

  • Its type conversion and validation behaviour did not match that of extension and built-in PHP functions
  • It followed a weak typing approach
  • Its attempt at “stricter” weak typing failed to placate either strict typing or weak typing fans

In creating this RFC, I have attempted to learn from these failings.

Weak typing and strict typing

There are two major approaches to how to check parameter and return type hints that have been proposed for PHP:

  • Strict type checking, which is used by many popular programming languages, particularly ones which are statically-typed, such as Java, C#, Haskell, or Facebook's Hack. It is also used for non-scalar parameter type hints in PHP. With this approach, an argument is only accepted if its type is exactly the same as the parameter. So, for example, an integer is accepted for an integer parameter, but a float is not accepted. Such systems often also accept subclasses, so if Bar inherits from Foo, it might be accepted for a Foo parameter.
  • Weak type checking (which implicitly converts arguments, where possible, to the correct type), which is used to a limited extent by C, and is also the approach by PHP's extension and built-in functions for scalar types. With this approach, along with values with exactly matching types, some values that can be converted are accepted. So, for example, an integer is accepted for an integer parameter, but a float or a numeric string might also be accepted, depending on the rules of the language. These convertible values will be converted when they are passed to the function. So, even though the float value 1.0 might have been passed, the value the function actually gets is an integer 1.

Both approaches have their advantages and disadvantages, and in fact PHP already has a mix of both. We use strict type checking for non-scalars such as arrays, objects and resources, and this applies to both userland type hints, and extension and built-in PHP function parameter types. We use weak type checking for scalar parameter types, but only for extension and built-in PHP functions, as PHP does not currently have scalar type hints.

In both approaches, the function will always get exactly the argument type it asks for. In the case of strict type-checking, this is done by rejecting incorrectly-typed values. In the case of weak type-checking, this is done by rejecting some values, and converting others. Therefore, the following code will always work, regardless of mode:

function foobar(int $i) {
    if (!is_int($i)) {
        throw new Exception("Not an integer."); // this will never happen
    }
}

Similarly, in both approaches, a function will always return exactly the return type it claims to:

function barfoo(): int {
   /* ... */
}
if (!is_int(barfoo())) {
    throw new Exception("Not an integer."); // this will also never happen
}

Why both?

So far, most advocates of scalar type hints have asked for either strict type checking, or weak type checking. Rather than picking one approach or the other, this RFC instead makes weak type checking the default, and adds an optional directive to use strict type checking within a file. There were several reasons behind this choice.

By and large the PHP community, myself included, seems to be in favour of strict type checking. However, adding strictly type-checked scalar type hints would cause a few problems:

  • It creates a glaring inconsistency: extension and built-in PHP functions use weak type checking for scalar typed parameters, yet userland PHP functions would be using strict type checking for scalar type hinted parameters.
  • The significant population who would like weak type checking would not be in favour of such a proposal, and are likely to block it.
  • Existing code which (perhaps unintentionally) took advantage of PHP's weak typing would break if functions it calls added scalar type hints to parameters. This would complicate the addition of scalar type hints to the parameters of functions in existing codebases, particularly libraries.

There is also a significant group of people (including, at times, my past self) who are in favour of weak type checking. But, like adding strictly type-checked hints, adding weakly type-checked scalar type hints would also cause problems:

  • The large number of people who would like strict type checking would not be in favour of such a proposal, and are likely to block it.
  • It would limit opportunities for static analysis.

A third approach has also been suggested, which is to add separate weakly- and strictly-checked type hints with different syntax. It would present its own set of issues:

  • People who do not like weak or strict type checking would be forced to deal with strictly or weakly type-checked libraries, respectively.
  • Like adding strict hints, this would also be inconsistent with extension and built-in PHP functions, which are uniformly weak.

In order to avoid the issues with these three approaches, this RFC proposes a fourth approach: per-file strict or weak type-checking. This has the following advantages:

  • People can choose the type checking model that suits them best, which means this approach should hopefully place both the strict and weak type checking camps.
  • APIs do not force a type hinting model upon their users.
  • Because files use the weak type checking approach by default, functions in existing codebases (including libraries) should be able to have scalar type hints added without breaking code that calls them. This enables codebases to add type hints gradually, or only to portions, which is known as “gradual typing”.
  • There only needs to be a single syntax for scalar type hints.
  • People who would prefer strict type checking get it not only for userland functions, but also for extension and built-in PHP functions. This means users get one model uniformly, rather than having the inconsistency that introducing strict-only scalar hints would have produced.
  • In strict type checking mode, the error level produced when type checking fails for extension and built-in PHP functions will finally be consistent with the error level produced for userland functions, with both producing E_RECOVERABLE_ERROR.

Type hint choices

No type hint for resources is added, as this would prevent moving from resources to objects for existing extensions, which some have already done (e.g. GMP).

For the integer typehint, both the int and integer syntaxes are allowed, and for the boolean typehint, both bool and boolean are allowed. This has been done because PHP uses both throughout the manual and error messages, so there is no clear choice of syntax that wouldn't cause problems. While in an ideal world we would not need to support these aliases, the likelihood of people being caught out by integer or boolean not working is very high, so I feel we ought to support both the short and long forms of these type names.

Details

Scalar type hints

No new reserved words are added. The names int, integer, float, string, bool and boolean are recognised and allowed as type hints, and prohibited from use as class/interface/trait names (including with use and class_alias).

The new userland scalar type hints are implemented internally by calling the Fast Parameter Parsing API functions.

strict_types declare() directive

By default, all PHP files are in weak type-checking mode. A new declare() directive is added, strict_types, which takes either 1 or 0. If 1, strict type-checking mode is used for function calls and return statements in the remainder of the file. If 0, weak type-checking mode is used.

This directive also supports the declare() block syntax (e.g. declare(strict_types=1) { foo(); }), in which case it will only affect function calls and return statements within the block.

Like the encoding directive, but unlike the ticks directive, the strict_types directive only affects the specific file it is used in, and does not affect either other files which include the file, nor other files that are included by the file.

The directive is entirely compile-time and cannot be controlled at runtime. It works by setting a flag on the opcodes for function calls (for parameter type hints) and return type checks (for return type hints).

Parameter type hints

The directive affects any function call, including those within a function or method. For example:

strict_types_scope.php
<?php
 
declare(strict_types=1) {
    foo(); // strictly type-checked function call
 
    function foobar() {
        foo(); // strictly type-checked function call
    }
 
    class baz {
        function foobar() {
            foo(); // strictly type-checked function call
        }
    }
}
 
foo(); // weakly type-checked function call
 
function foobar() {
    foo(); // weakly type-checked function call
}
 
class baz {
    function foobar() {
        foo(); // weakly type-checked function call
    }
}

Whether or not the function being called was declared in a file that uses strict or weak type checking is irrelevant. The type checking mode depends on the file where the function is called.

Return type hints

The directive affects any return statement in any function or method within a file. For example:

strict_types_scope.php
<?php
 
declare(strict_types=1) {
    function foobar(): int {
        return 1.0; // strictly type-checked return
    }
 
    class baz {
        function foobar(): int {
            return 1.0; // strictly type-checked return
        }
    }
}
 
function foobar(): int {
    return 1.0; // weakly type-checked return
}
 
class baz {
    function foobar() {
        return 1.0; // weakly type-checked return
    }
}

Unlike parameter type hints, the type checking mode used for return types depends on the file where the function is defined, not where the function is called. This is because returning the wrong type is a problem with the callee, while passing the wrong type is a problem with the caller.

Behaviour of weak type checks

A weakly type-checked call to an extension or built-in PHP function has exactly the same behaviour as it did in previous PHP versions.

The weak type checking rules for the new scalar type hints are mostly the same as those of extension and built-in PHP functions. The only exception to this is the handling of NULL: in order to be consistent with our existing type hints for classes, callables and arrays, NULL is not accepted by default, unless it is a parameter and is explicitly given a default value of NULL. This would work well with the draft Declaring Nullable Types RFC. If that RFC were to pass, it would be possible to mark return types as nullable, and so they would accept NULL.

For the reference of readers who may not be familiar with PHP's existing weak scalar parameter type rules, the following brief summary is provided.

The table shows which types are accepted and converted for scalar type hints. NULL, arrays and resources are never accepted for scalar type hints, and so are not included in the table.

Type hint integer float string boolean object
integer yes yes* yes† yes no
float yes yes yes† yes no
string yes yes yes yes yes‡
boolean yes yes yes yes no

*Only non-NaN floats between PHP_INT_MIN and PHP_INT_MAX accepted. (New in PHP 7, see the ZPP Failure on Overflow RFC)

†Non-numeric strings not accepted. Numeric strings with trailing characters are accepted, but produce a notice.

‡Only if it has a __toString method.

Behaviour of strict type checks

A strictly type-checked call to an extension or built-in PHP function changes the behaviour of zend_parse_parameters. In particular, it will produce E_RECOVERABLE_ERROR rather than E_WARNING on failure, and it follows strict type checking rules for scalar typed parameters, rather than the traditional weak type checking rules.

The strict type checking rules are quite straightforward: when the type of the value matches that specified by the type hint it is accepted, otherwise it is not.

These strict type checking rules are used for userland scalar type hints, and for extension and built-in PHP functions.

Backward Incompatible Changes

int, integer, float, string, bool and boolean are no longer permitted as class/interface/trait names (including with use and class_alias).

Because the weak type-checking rules for scalar hints are quite permissive in the values they accept and behave similarly to PHP's type juggling for operators, it should be possible for existing userland libraries to add scalar type hints without breaking compatibility.

Since the strict type-checking mode is off by default and must be explicitly used, it does not break backwards-compatibility.

Proposed PHP Version(s)

This is proposed for the next PHP x, currently PHP 7.

RFC Impact

To Existing Extensions

ext/reflection will need to be updated in order to support scalar type hint reflection for parameters. This hasn't yet been done.

Unaffected PHP Functionality

This doesn't affect the behaviour of cast operators.

When the strict type-checking mode isn't in use (which is the default), function calls to built-in and extension PHP functions behave identically to previous PHP versions.

Open Issues

There is currently an open issue related to naming, described below. This will go to a vote.

This RFC and patch allows the aliases integer and boolean in addition to int and bool. Should we only allow int and bool? It is probably not a good idea to add too many new reserved class names. On the other hand, we use integer and boolean in many places in the manual, and programmers would be forgiven for expecting integer and boolean to work. We could opt to reserve them but prevent their use, telling people to use int and bool instead. That wouldn't reduce the number of prohibited class names, but it would prevent confusion and ensure consistency.

TODO

  • Produce a different error message on strict type hint failure vs. on weak type hint failure, lest debugging be a pain. For practical reasons we may not be able to change the weak one, as it is an existing message (should we?).
  • Return types need more tests.

Future Scope

Because scalar type hints guarantee that a passed argument will be of a certain type within a function body (at least initially), this could be used in the Zend Engine for optimisations. For example, if a function takes two float-hinted arguments and does arithmetic with them, there is no need for the arithmetic operators to check the types of their operands. As I understand it, HHVM already does such optimisations, and might benefit from this RFC.

In discussions around this RFC, the declare(strict_types=1); syntax has been controversial. It is arguably rather ugly, and it has all the scoping quirks of declare(). I've now arrived at a point where I'm willing to change to a truly per-file syntax, <?php strict (or similar). However, the RFC is currently being voted on, and I'd rather not cancel the vote. So, this will be proposed in a subsequent RFC.

Another issue that came up is PHP's lack of a typehint for numbers (which also came up with the previous Scalar Type Hinting with Cast RFC). I plan to propose a numeric typehint in a future RFC which would take either an integer or a float.

Vote

As this is a language change, this RFC requires a 2/3 majority to pass. Voting for all three votes started on 2015-02-05 and all were to end on 2015-02-19, but the voting was cancelled and the RFC withdrawn on 2015-02-15.

Patches and Tests

There is a working, but possibly buggy php-src pull request with tests here: https://github.com/php/php-src/pull/998

There is no language specification patch as yet.

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

  • Previous discussions on the internals mailing list about scalar type hinting: one, two, three, four

Versions

Version Changed Date
0.3.1 Noted numeric and syntax change in Future Scope
0.3 Return types support
0.2.3 strict_types=1 rather than strict_typehints=TRUE
0.2.2 Follow robustness principle for return types under Future Scope
0.2.1 Weak typing clarification
0.2 Introduction of optional strict hinting mode
0.1.2 Noted some downsides of strict hints vs weak hints
0.1.1 Added table summarising casting and validation rules
0.1 Initial drafts

Votes

An option needs 2/3 votes to win

Accept the Scalar Type Hints RFC and merge patch into master? (66.3% approved)
User Vote
ab Yes
aharvey No
ajf Yes
alan_k No
andi No
ashnazg Yes
bd808 No
ben Yes
blanchonvincent Yes
brandon Yes
brianlmoon Yes
bwoebi No
chregu Yes
chx Yes
colder Yes
crodas No
cyberline Yes
danack No
daverandom Yes
davey Yes
demon No
derick Yes
diegopires Yes
dm Yes
dmitry No
dragoonis No
dsp No
duodraco Yes
Fabien Potencier Yes
fernandoc Yes
fmargaine Yes
francois Yes
galvao Yes
guilhermeblanco Yes
gwynne Yes
hywan Yes
indeyets Yes
ircmaxell Yes
irker Yes
ivonascimento Yes
jedibc Yes
jgmdev Yes
jmikola Yes
jpauli Yes
jwage Yes
kalle No
keyur No
kinncj Yes
klaussilveira Yes
krakjoe Yes
laruence No
lcobucci Yes
leigh No
levim No
lstrojny No
malukenho Yes
marco Yes
mbeccati Yes
mfischer No
mfonda No
mike No
mrook Yes
neeke No
nikic Yes
olemarkus No
pajoye Yes
patrickallaert No
pauloelr Yes
peehaa No
philstu Yes
pollita Yes
rafa Yes
ramsey Yes
rasmus No
rdlowrey Yes
rdohms Yes
rogeriopradoj Yes
royopa Yes
salathe Yes
sebastian No
seld Yes
stas No
stelianm Yes
stewartlord Yes
subjective Yes
tacker Yes
thekid No
thorstenr Yes
till Yes
toby Yes
tony2001 No
treffynnon Yes
tyrael No
weierophinney Yes
wiesemann Yes
willfitch Yes
yohgaki No
yumin1985 No
yunosh Yes
zeev No
zhangzhenyu No
Type aliases (77% approved)
User Vote
ab Reserve synonyms and produce error message when used
aharvey Allow synonyms
ajf Reserve synonyms and produce error message when used
alan_k Do not reserve
andi Do not reserve
ashnazg Reserve synonyms and produce error message when used
bd808 Allow synonyms
ben Do not reserve
blanchonvincent Allow synonyms
brandon Allow synonyms
brianlmoon Allow synonyms
bwoebi Allow synonyms
chregu Allow synonyms
chx Allow synonyms
crodas Do not reserve
cyberline Allow synonyms
danack Do not reserve
daverandom Allow synonyms
davey Allow synonyms
diegopires Allow synonyms
dm Allow synonyms
dmitry Do not reserve
dragoonis Do not reserve
duodraco Allow synonyms
Fabien Potencier Allow synonyms
fernandoc Allow synonyms
francois Allow synonyms
galvao Allow synonyms
guilhermeblanco Reserve synonyms and produce error message when used
gwynne Reserve synonyms and produce error message when used
hywan Allow synonyms
indeyets Allow synonyms
irker Reserve synonyms and produce error message when used
ivonascimento Allow synonyms
jedibc Allow synonyms
jgmdev Allow synonyms
jmikola Allow synonyms
jpauli Allow synonyms
kalle Allow synonyms
keyur Allow synonyms
kinncj Allow synonyms
klaussilveira Reserve synonyms and produce error message when used
krakjoe Do not reserve
laruence Do not reserve
lcobucci Allow synonyms
leigh Do not reserve
lstrojny Allow synonyms
malukenho Allow synonyms
marco Reserve synonyms and produce error message when used
mbeccati Reserve synonyms and produce error message when used
mfischer Reserve synonyms and produce error message when used
mike Do not reserve
mrook Reserve synonyms and produce error message when used
neeke Do not reserve
nikic Do not reserve
pajoye Allow synonyms
patrickallaert Do not reserve
pauloelr Allow synonyms
peehaa Allow synonyms
philstu Reserve synonyms and produce error message when used
pollita Reserve synonyms and produce error message when used
rafa Allow synonyms
ramsey Allow synonyms
rasmus Do not reserve
rdlowrey Allow synonyms
rdohms Reserve synonyms and produce error message when used
rogeriopradoj Reserve synonyms and produce error message when used
royopa Allow synonyms
salathe Allow synonyms
sebastian Reserve synonyms and produce error message when used
seld Reserve synonyms and produce error message when used
stas Allow synonyms
stelianm Reserve synonyms and produce error message when used
stewartlord Allow synonyms
subjective Allow synonyms
tacker Do not reserve
thekid Do not reserve
thorstenr Allow synonyms
till Do not reserve
toby Reserve synonyms and produce error message when used
tony2001 Do not reserve
treffynnon Allow synonyms
weierophinney Allow synonyms
wiesemann Allow synonyms
willfitch Allow synonyms
yohgaki Do not reserve
yunosh Allow synonyms
Reserve type names if RFC does not pass? (80.9% approved)
User Vote
ab Yes
aharvey Yes
ajf Yes
alan_k No
andi No
ashnazg Yes
bd808 Yes
ben Yes
blanchonvincent Yes
brandon Yes
brianlmoon Yes
bwoebi Yes
chregu Yes
chx Yes
crodas No
cyberline Yes
danack No
daverandom Yes
davey Yes
demon No
diegopires Yes
dmitry No
dragoonis No
duodraco Yes
Fabien Potencier Yes
fernandoc Yes
francois Yes
fredemmott Yes
galvao Yes
guilhermeblanco Yes
gwynne Yes
hradtke Yes
hywan Yes
indeyets Yes
irker Yes
ivonascimento Yes
jedibc Yes
jgmdev Yes
jmikola Yes
jpauli No
jwage Yes
kalle No
kinncj Yes
klaussilveira Yes
krakjoe No
lcobucci Yes
leigh Yes
lstrojny Yes
malukenho Yes
marco Yes
mbeccati Yes
mfischer Yes
mfonda No
mike No
mrook Yes
olemarkus Yes
pajoye Yes
patrickallaert Yes
pauloelr Yes
peehaa Yes
philstu Yes
pollita Yes
rafa Yes
ramsey Yes
rasmus No
rdlowrey Yes
rdohms Yes
rogeriopradoj Yes
royopa Yes
salathe Yes
sebastian Yes
seld Yes
stas No
stelianm Yes
stewartlord Yes
subjective Yes
tacker Yes
thekid No
thorstenr Yes
till Yes
toby Yes
tony2001 No
treffynnon Yes
weierophinney Yes
wiesemann Yes
willfitch Yes
yohgaki Yes
yunosh Yes
zeev No