1.2.970. Php 8.0 Only TypeHints¶
Three scalar typehints are introduced in version 8.0. They are
mixed
,false
andnull
.
false
represents a false boolean, and nothing else. It is more restrictive than a boolean, which accepts true too.
null
is an alternative syntax to ?
: it allows the type to be null
.
mixed
is an special typehint which explicitly means any type.
An interface stringable
was also introduced to identify objects that may be turned into a string.
Both the above typehints are to be used in conjunction with other types : they can’t be used alone.
In PHP 7.0, both those values could not be used as a class or interface name, to avoid confusion with the actual booleans, nor null
value.
<?php
// function accepts an A object, or null.
function foo(A|null $x) {}
// same as above
function foo2(A|null $x) {}
// returns an object of class B, or false
function bar($x) : false|B {}
?>
See also PHP RFC: Union Types 2.0.
1.2.970.1. Specs¶
Short name |
Php/Php80OnlyTypeHints |
Rulesets |
All, Appinfo, CE, Changed Behavior, CompatibilityPHP56, CompatibilityPHP70, CompatibilityPHP71, CompatibilityPHP72, CompatibilityPHP73, CompatibilityPHP74 |
Exakat since |
2.0.9 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Features |
mixed, false, null |
Available in |