Abdullah Abdullah - 3 months ago 7x
PHP Question

Cannot pass null argument when using type hinting

The following code:


class Type {


function foo(Type $t) {




failed at run time:

PHP Fatal error: Argument 1 passed to foo() must not be null

Why is it not allowed to pass null just like other languages?


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.