php8.5
Home/ Manual/ reflection / reflectiontype/ ReflectionType::allowsNull

ReflectionType::allowsNull

PHP function Edit on GitHub ✎

(PHP 7, PHP 8)

Checks if null is allowed

Description

ReflectionType::allowsNull(): bool

Checks whether the parameter allows null.

Parameters Parameters

Return Values

true if null is allowed, otherwise false

Examples

ReflectionType::allowsNull example

php
<?php
function someFunction(string $param, stdClass $param2 = null) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();

var_dump($reflectionParams[0]->getType()->allowsNull());
var_dump($reflectionParams[1]->getType()->allowsNull());

The above example will output:

output
bool(false)
bool(true)

See Also

  • ReflectionNamedType::isBuiltin
  • ReflectionType::__toString
  • ReflectionParameter::getType

Source: reference/reflection/reflectiontype/allowsnull.xml · from the official PHP manual (php/doc-en)