PHP 8.4.6 Released!

ReflectionParameter::allowsNull

(PHP 5, PHP 7, PHP 8)

ReflectionParameter::allowsNullChecks if null is allowed

Description

public ReflectionParameter::allowsNull(): bool

Checks whether the parameter allows null.

Parameters

This function has no parameters.

Return Values

true if null is allowed, otherwise false

See Also

User Contributed Notes

Geoffrey LAURENT
11 years ago
The allowsNull method look if arguments have a type.
If a type is defined, null is allowed only if default value is null.

<?php
function myfunction ( $param ) {

}

echo (new
ReflectionFunction("myfunction"))->getParameters()[0]->allowsNull() ? "true":"false";

?>

Result : true

<?php
function myfunction ( stdClass $param ) {

}

echo (new
ReflectionFunction("myfunction"))->getParameters()[0]->allowsNull() ? "true":"false";

?>

Result : false

<?php
function myfunction ( stdClass $param = null ) {

}

echo (new
ReflectionFunction("myfunction"))->getParameters()[0]->allowsNull() ? "true":"false";
?>

Result : true
To Top