Cannot Pass Null Argument When Using Type Hinting

Cannot pass null argument when using type hinting

PHP 7.1 or newer (released 2nd December 2016)

You can explicitly declare a variable to be null with this syntax

function foo(?Type $t) {
}

this will result in

$this->foo(new Type()); // ok
$this->foo(null); // ok
$this->foo(); // error

So, if you want an optional argument you can follow the convention Type $t = null whereas if you need to make an argument accept both null and its type, you can follow above example.

You can read more here.


PHP 7.0 or older

You have to add a default value like

function foo(Type $t = null) {

}

That way, you can pass it a null value.

This is documented in the section in the manual about Type Declarations:

The declaration can be made to accept NULL values if the default value of the parameter is set to NULL.

PHP: How do I fix this type hinting error?

From the manual

Typed pass-by-reference Parameters

Declared types of reference parameters are checked on function entry, but not when the function returns, so after the function had returned, the argument's type may have changed.

Given you are immediately assigning a new value to $output, its type declaration is irrelevant. Either omit the type declaration or mark it nullable

function foo(int $one, int $two, ?int &$output): void
{
$output = $one + $two;
}

https://3v4l.org/j91DG


Of course, this type of pattern is convoluted and makes no sense over something as simple as

function foo(int $one, int $two): int
{
return $one + $two;
}

Type Hinting: Default Parameters

You can't typehint strings, you can only typehint objects and arrays, so this is incorrect:

function setName ( string $name = "happ") {

(The reason you don't get a compile-time error here is because PHP is interpreting "string" as the name of a class.)

The wording in the docs means that if you do this:

function foo(Foo $arg) {

Then the argument passed to foo() must be an instance of object Foo. But if you do this:

function foo(Foo $arg = null) {

Then the argument passed to foo() can either be an instance of object Foo, or null. Note also that if you do this:

function foo(array $foo = array(1, 2, 3))

Then you can't call foo(null). If you want this functionality, you can do something like this:

function foo(array $foo = null) {
if ($foo === null) {
$foo = array(1, 2, 3);
}

[Edit 1] As of PHP 5.4, you can typehint callable:

function foo(callable $callback) {
call_user_func($callback);
}

[Edit 2] As of PHP 7.0, you can typehint bool, float, int, and string. This makes the code in the question valid syntax. As of PHP 7.1, you can typehint iterable.

Should I use the intended type when using type hinting for function result?

When it says "expected type" it means that type checkers should warn when the actual type doesn't meet the expectation.

Union types can be used when a parameter or return value can be multiple types. And the specific case of allowing None as a placeholder for a nonexistent value is handled using the Optional type. So you would write:

def get_first_name(last_name: str) -> Optional[str]:

What happens when I provide type hint but don't initialize?

You're referring to type annotations, as defined by PEP 526:

my_var: int

Please note that type annotations differ from type hints, as defined by PEP 428:

def my_func(foo: str):
...

Type annotations have actual runtime effects. For example, the documentation states:

In addition, at the module or class level, if the item being annotated is a simple name, then it and the annotation will be stored in the __annotations__ attribute of that module or class [...]

So, by slightly modifying your example, we get this:

>>> class T:
... a: str
...
>>> T.__annotations__
{'a': <class 'str'>}


Related Topics



Leave a reply



Submit