Content · Programming · PHP

PHP ReflectionType::allowsNull() Method: Usage and Examples

Reports whether a reflected type permits null.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

When building tools that inspect PHP code, ReflectionType::allowsNull() helps answer a focused question: reports whether a reflected type permits null.

Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.

What Is ReflectionType::allowsNull()?


Reports whether a reflected type permits null.

Its method signature is:

public function allowsNull(): bool

This method has no required arguments.

Return value: bool

Example 1: Inspect the basic behavior


Start with a self-contained PHP declaration and inspect the method result.

<?php

declare(strict_types=1);

function inspectValue(?string $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();

$result = $typeReflection->allowsNull();
var_dump($result);

Output:

bool(true)

Explanation: Reports whether a reflected type permits null.

Example 2: Compare a second case


Next, change the reflected declaration or parameter to observe a related situation.

<?php

declare(strict_types=1);

function inspectValue(int $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();

$result = $typeReflection->allowsNull();
var_dump($result);

// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);

Output:

bool(false)

Result type: bool

Explanation: Union types containing null behave differently from nonnullable named types.

Practical Example: Apply the operation in context


This final case shows how the same API can be used when inspecting application definitions.

<?php

declare(strict_types=1);

function inspectValue(string|null $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();

$result = $typeReflection->allowsNull();
var_dump($result);

// Use the reflection result in a simple decision.
if (is_bool($result)) {
    echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
    echo "\nEntries: " . count($result);
} elseif ($result === null) {
    echo "\nNo value returned";
} else {
    echo "\nReflection result available";
}

Output:

bool(true)

Status: yes

Explanation: ReflectionNamedType::getName reports the type name only.

Important Behavior and Edge Cases


  • Union types containing null behave differently from nonnullable named types.
  • ReflectionNamedType::getName reports the type name only.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionType::allowsNull()Reports whether a reflected type permits null.
Related APIReflectionNamedType::getName reports the type name only.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: Union types containing null behave differently from nonnullable named types.Handle the specific limitation: Union types containing null behave differently from nonnullable named types.
Confusing this method with Related APIReflectionNamedType::getName reports the type name only.

Key Takeaways


  • Reports whether a reflected type permits null.
  • Union types containing null behave differently from nonnullable named types.
  • ReflectionNamedType::getName reports the type name only.

Use ReflectionType::allowsNull() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.

Share this resourceSend this page to someone who may find it useful.