Overview
Type declarations make function and method contracts clearer and help catch invalid values. PHP supports parameter types, return types, property types and several compound type forms.
Common forms
- Scalar types include
int,float,stringandbool. - Class and interface names constrain object values.
- Nullable types use
?Typewhere supported by the target PHP version. - Union types allow one of several declared types.
- Intersection types express values that satisfy multiple class or interface types.
mixedaccepts any value; use it deliberately.
Strict typing
declare(strict_types=1) affects scalar type coercion for calls made from the file where it is declared. Understand its scope and test public interfaces carefully.
Compatibility
Check the minimum PHP version for every syntax feature used by the project.