CakeFest 2024: The Official CakePHP Conference

ReflectionParameter::allowsNull

(PHP 5, PHP 7, PHP 8)

ReflectionParameter::allowsNullVérifie si la valeur null est autorisée comme valeur du paramètre

Description

public ReflectionParameter::allowsNull(): bool

Vérifie si la valeur null est autorisée comme valeur du paramètre.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

true si la valeur null est autorisée comme valeur du paramètre, false sinon.

Voir aussi

add a note

User Contributed Notes 2 notes

up
15
Geoffrey LAURENT
10 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
up
0
tuncdan dot ozdemir dot peng at gmail dot com
1 month ago
Please note that `mixed` type parameter also returns true, as `null` is part of the `mixed` union.

And there does not have to be a default `null` value for `->allowsNull()` to return true.

function test (AnyType|null $param1, mixed $param2) {}

Both parameters from the function above will return true for `allowsNull()`.
To Top