Type hinting scalar types arrays and callables
suggest changeSupport for type hinting array parameters was added in PHP 5.1 with the keyword array. Return types, including array, were added in PHP 7.0. Any array of any dimension and type, as well as an empty array, is a valid value.
Support for type hinting callables was added in PHP 5.4. A callable return type also requires PHP 7.0. Any value for which is_callable() is true is valid, i.e. Closure objects, function name strings and array(class_name|object, method_name).
If the value is not callable, PHP raises a TypeError. On PHP 8 the message looks like this:
Fatal error: Uncaught TypeError: foo(): Argument #1 ($c) must be of type callable, string given
function foo(callable $c) {}
foo("count"); // valid
foo("Phar::running"); // valid
foo(["Phar", "running"]); // valid
foo([new ReflectionClass("stdClass"), "getName"]); // valid
foo(function() {}); // valid
foo("no_such_function"); // callable expected, string given
Nonstatic methods can also be passed as callables in static format, resulting in a deprecation warning and level E_STRICT error in PHP 7 and 5 respectively.
Method visibility is taken into account. If the context of the method with the callable parameter does not have access to the callable provided, it will end up as if the method does not exist.
class Foo{
private static function f(){
echo "Good" . PHP_EOL;
}
public static function r(callable $c){
$c();
}
}
function r(callable $c){}
Foo::r(["Foo", "f"]);
r(["Foo", "f"]);
Foo::r() can see the private method, so it prints Good and then the global r() raises:
Good
Fatal error: Uncaught TypeError: r(): Argument #1 ($c) must be of type callable, array given
Support for type hinting scalar types was added in PHP 7. The type names are bool, int, float, and string.
<?php
function add(int $a, int $b) {
return $a + $b;
}
var_dump(add(1, 2)); // Outputs "int(3)"
By default, PHP will attempt to cast any provided argument to match its type hint. Changing the call to add(1.5, 2) still prints int(3), because the float 1.5 was truncated to int. Since PHP 8.1 that truncation is deprecated when it loses precision.
To stop this behavior, one must add declare(strict_types=1); to the top of every PHP source file that requires it.
<?php
declare(strict_types=1);
function add(int $a, int $b) {
return $a + $b;
}
var_dump(add(1.5, 2));
The above script now produces a fatal error:
Fatal error: Uncaught TypeError: add(): Argument #1 ($a) must be of type int, float given
An Exception: Special Types
Some PHP functions may return a value of type resource. There is no resource type declaration: a resource hint is read as a class name. On PHP 8, PHP also warns that resource is not a built-in type.
fopen() still returns a stream resource. curl_init() returned a resource before PHP 8 and returns a CurlHandle object since PHP 8. On PHP 7, hinting resource and passing a resource produced this TypeError:
TypeError: Argument 1 passed to sample() must be an instance of resource, resource given