Codestin Search App
Type declarations can be added to function arguments, return values,
as of PHP 7.4.0, class properties, and as of PHP 8.3.0, class constants.
They ensure that the value is of the specified type at call time,
otherwise a TypeError is thrown.
Every single type that PHP supports, with the exception of
resource can be used within a user-land type declaration.
This page contains a changelog of availability of the different types
and documentation about usage of them in type declarations.
When a class implements an interface method or reimplements a method which
has already been defined by a parent class, it has to be compatible with the
aforementioned definition.
A method is compatible if it follows the
variance rules.
&reftitle.changelog;
&Version;&Description;8.3.0
Support for class, interface, trait, and enum constant typing has been added.
8.2.0
Support for DNF types has been added.
8.2.0
Support for the literal type true has been added.
8.2.0
The types null and false can now be used standalone.
8.1.0
Support for intersection types has been added.
8.1.0
Returning by reference from a void function is now deprecated.
8.1.0
Support for the return only type never has been added.
8.0.0
Support for mixed has been added.
8.0.0
Support for the return only type static has been added.
8.0.0
Support for union types has been added.
7.4.0
Support for class properties typing has been added.
7.2.0
Support for object has been added.
7.1.0
Support for iterable has been added.
7.1.0
Support for void has been added.
7.1.0
Support for nullable types has been added.
Codestin Search App
Atomic types have straight forward behaviour with some minor caveats which
are described in this section.
Codestin Search App
Name aliases for scalar types (bool, int,
float, string) are not supported.
Instead, they are treated as class or interface names.
For example, using boolean as a type declaration
will require the value to be an &instanceof; the class or interface
boolean, rather than of type bool:
]]>
&example.outputs.8;
Codestin Search App
Returning by reference from a void function is deprecated as of PHP 8.1.0,
because such a function is contradictory.
Previously, it already emitted the following
E_NOTICE when called:
Only variable references should be returned by reference.
]]>
Codestin Search App
This type cannot be used as a class property type declaration.
It is not possible to specify the signature of the function.
Codestin Search App
If a pass-by-reference parameter has a type declaration, the type of the
variable is only checked on function entry, at the
beginning of the call, but not when the function returns.
This means that a function can change the type of variable reference.
Codestin Search App
]]>
&example.outputs.similar;
Codestin Search App
Composite type declarations are subject to a couple of restrictions and
will perform a redundancy check at compile time to prevent simple bugs.
Prior to PHP 8.2.0, and the introduction of DNF types,
it was not possible to combine intersection types with union types.
Codestin Search App
It is not possible to combine the two singleton types false
and true together in a union type.
Use bool instead.
Prior to PHP 8.2.0, as false and null
could not be used as standalone types, a union type comprised of only
these types was not permitted. This comprises the following types:
false, false|null,
and ?false.
Codestin Search App
A single base type declaration can be marked nullable by prefixing the
type with a question mark (?).
Thus ?T and T|null are identical.
This syntax is supported as of PHP 7.1.0, and predates generalized union
types support.
It is also possible to achieve nullable arguments by making
null the default value.
This is not recommended as if the default value is changed in a child
class a type compatibility violation will be raised as the
null type will need to be added to the type declaration.
This behavior is also deprecated since PHP 8.4.
Codestin Search App
]]>
&example.outputs;
Codestin Search App
To catch simple bugs in composite type declarations, redundant types that
can be detected without performing class loading will result in a
compile-time error. This includes:
Each name-resolved type may only occur once. Types such as
int|string|INT or
Countable&Traversable&COUNTABLE
result in an error.
Using mixed or never results in an error.
For union types:
If bool is used, false or true
cannot be used additionally.
If object is used, class types cannot be used additionally.
If iterable is used, array
and Traversable cannot be used additionally.
For intersection types:
Using a type which is not a class-type results in an error.
Using either self, parent, or
static results in an error.
For DNF types:
If a more generic type is used, the more restrictive one is redundant.
Using two identical intersection types.
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.
]]>
&reftitle.examples;
Codestin Search App
]]>
&example.outputs.8;
Codestin Search App
]]>
&example.outputs.8;
Codestin Search App
]]>
&example.outputs;
Codestin Search App
]]>
&example.outputs;
Codestin Search App
]]>
&example.outputs;
Codestin Search App
]]>
Codestin Search App
id = $id;
$this->username = $username;
}
}
?>
]]>
Codestin Search App
By default, PHP will coerce values of the wrong type into the expected
scalar type declaration if possible. For example, a function that is given
an int for a parameter that expects a string
will get a variable of type string.
It is possible to enable strict mode on a per-file basis. In strict
mode, only a value corresponding exactly to the type declaration will be
accepted, otherwise a TypeError will be thrown.
The only exception to this rule is that an int value will
pass a float type declaration.
Function calls from within internal functions will not be affected by
the strict_types declaration.
To enable strict mode, the &declare; statement is used with the
strict_types declaration:
Strict typing applies to function calls made from
within the file with strict typing enabled, not to
the functions declared within that file. If a file without strict
typing enabled makes a call to a function that was defined in a file
with strict typing, the caller's preference (coercive typing) will be
respected, and the value will be coerced.
Strict typing is only defined for scalar type declarations.
Codestin Search App
]]>
&example.outputs.8;
Codestin Search App
]]>
&example.outputs;
Codestin Search App
]]>
&example.outputs;