This is an old revision of the document!
PHP RFC: Add true type
- Version: 0.1
- Date: 2022-04-7
- Author: George Peter Banyard, girgias@php.net
- Status: Draft
- Target Version: PHP 8.2
- Implementation: https://github.com/php/php-src/pull/8326
- First Published at: http://wiki.php.net/rfc/true-type
Introduction
PHP now has support for ''null'' and ''false'' as standalone types.
However, true
which is the natural counter part of false
does not even exist as a type.
The motivations are the same to previous RFC targeting the type system:
- Type system completeness
- Providing precise type information while satisfying LSP
Proposal
Add support for using true
as a type declaration, wherever type declarations are currently allowed.
class Truthy { public true $truthy = true; public function foo(true $v): true { /* ... */ *} }
Compile time redundancy checks
As it is the case currently for false
a compile time error will be emitted if true
and bool
are present in union.
For example, the following function signature:
<?php function test(): bool|true {} ?>
Will emit the following error:
Fatal error: Duplicate type true is redundant in %s on line %d
A compile time error is also emitted when true|false
is used instead of bool
.
The following function signature:
<?php function test(): false|true {} ?>
will emit the following error:
Fatal error: Type contains both true and false, bool should be used instead in %s on line %d
Examples
There are many examples of functions which only return true
.
Example of internal functions are all the array_sort
functions always return true
.
And examples of userland projects which can benefit of this change is Composer, which has various cases where they could use a true
type.
[1]
[2]
[3]
[4]
Reflection
The new true
type will behave as any other type in regards to Reflection.
Backward Incompatible Changes
This RFC does not contain any backwards incompatible changes.
Proposed PHP Version
Next minor version, i.e. PHP 8.2.
Proposed Voting Choices
As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
Voting started on 2022-03-12 and will end on 2022-03-26.
Implementation
GitHub pull request: https://github.com/php/php-src/pull/8326
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