About PHP types 


php128 apg

In PHP coding, the following types considered as primitives are supported:


 SCALAR TYPES 


Four SCALAR TYPES:

a) bool,
b) int,
c) flat, (floating-point number aka double )
and
d) string



 COMPOUND TYPES 


Four COMPOUND TYPES:

e) array,
f) object,
g) callable
Callbacks can be denoted by the callable type declaration.
and
h) iterable
Iterable can be used as a parameter type to indicate that a function requires a set of values, but does not care about the form of the value set since it will be used with foreach.



 SPECIAL TYPES 


Two SPECIAL TYPES:

i) resource
and
j) NULL



 PSEUDO TYPES 


Five PSEUDO TYPES:

a) mixed,
b) number,
c) callback,
d) array|object
and
e) void





Type Description Version
Class/interface name The value must be an instanceof the given class or interface  
self The value must be an instanceof the same class as the one
in which the type declaration is used.
Can only be used in classes.
 
parent The value must be an instanceof the parent of the class
in which the type declaration is used.
Can only be used in classes.
 
array The value must be an array.  
callable The value must be a valid callable.
Cannot be used as a class property type declaration.
 
bool The value must be a boolean value.  
float The value must be a floating point number.  
int The value must be an integer.  
string The value must be a string.  
iterable The value must be either an array or an instanceof Traversable. PHP 7.1.0
object The value must be an object. PHP 7.2.0
mixed

union
The value can be any value.
equivalent to the union type:

array|bool|callable|int|float |object|resource|string|null
PHP 8.0.0
php.net




An union type declaration accepts values of multiple different types, rather than a single one.

union type is specified using the syntax T1|T2|....

union type is available as of PHP 8.0.0.

The null type is supported as part of unions, such that T1|T2|null can be used to create a nullable union.

The existing ?T notation is considered a shorthand for the common case of T|null.

null cannot be used as a standalone type.

The false literal type is supported as part of unions, and is included as for historical reasons many internal functions return false instead of null for failures.

false cannot be used as a standalone type (including nullable standalone type).

As such, all of false, false|null and ?false are not permitted.

The true literal type does not exist.


To catch simple bugs in union type declarations, redundant types that can be detected without performing class loading will result in a compile-time error.

This includes:

1. Each name-resolved type may only occur once.
Types such as int|string|INT result in an error.

2. If bool is used, false cannot be used additionally.

3. If object is used, class types cannot be used additionally.

4. If iterable is used, array and Traversable cannot be used additionally.


This does not guarantee that the type is “minimal”, because doing so would require loading all used class types.

For example, if A and B are class aliases, then A|B remains a legal union type, even though it could be reduced to either A or B.

Similarly, if class B extends A {}, then A|B is also a legal union type, even though it could be reduced to just A.

We will study each of these types in due course.