This is an old revision of the document!
PHP RFC: Scalar Pseudo-type
- Version: 1.0
- Date: 2017-12-24 (use today's date here)
- Author: Richard Fussenegger, php@fleshgrinder.com
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/scalar-pseudo-type
Introduction
Some procedures are capable of accepting or returning any type of the ones that are considered scalar. PHP does not have support for union types and the only way to communicate such support to callers or receivers is via documentation. This RFC proposes the addition of a special scalar
pseudo-type that covers all types that are considered scalar
– namely bool
, float
, int
, and string
– to be valid for both parameter and return type constraints.
Proposal
This RFC proposes a new scalar
pseudo-type. This type is analogous to callable
and iterable
, accepting multiple types instead of one single type.
scalar
accepts bool
, float
, int
, and string
. All of these types can be safely coerced to a string
and be printed.
scalar
can be used as a parameter type constraint to require that a procedure is called with any of the types that are considered scalar.
function f(scalar $param) { echo "{$param}\n"; }
scalar
can be combined with the nullable constraint to broaden the amount of types that are accepted.
function f(?scalar $param) { echo "{$param}\n"; }
scalar
can also be used as a return type constraint to indicate that a procedure will return any of bool
, float
, int
, or string
. The combination with the nullable constraint is supported here as well.
function f(): scalar { return 42; } function f(): ?scalar { return null; }
Parameters that are constrained to scalar
may use a bool
, float
, int
, null
, or string
as default value.
function f0(scalar $p = null) {} function f1(scalar $p = true) {} function f2(scalar $p = 4.2) {} function f3(scalar $p = 42) {} function f4(scalar $p = 'str') {}
Classes extending or implementing a supertype that has a scalar
compatible parameter type constraint may broaden that constraint to be scalar
(contravariance).
interface A { function f1(bool $p); function f2(float $p); function f3(int $p); function f4(string $p); } interface B implements A { function f1(scalar $p); function f2(scalar $p); function f3(scalar $p); function f4(scalar $p); }
Classes extending or implementing a supertype that has a scalar
return type constraint may narrow that constraint down to a compatible type (covariance).
interface A { function f(): scalar; } interface B extends A { function f(): bool; } interface C extends A { function f(): float; } interface D extends A { function f(): int; } interface E extends A { function f(): string; }
The function is_scalar to determine whether a value is scalar
or not already exist in PHP since a long time and must not be added.
Backward Incompatible Changes
scalar
is implemented as reserved name, therefore a class, interface, or trait named scalar
cannot be declared.
Proposed PHP Version(s)
7.3.0
Future Scope
- Addition of an interface that allows objects to be considered
scalar
.
Proposed Voting Choices
This proposal requires a 2/3 majority to be accepted.