Content · Programming · PHP

PHP ReflectionParameter::getAttributes() Method: Usage and Examples

Returns PHP attributes attached specifically to the parameter.

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

When building tools that inspect PHP code, ReflectionParameter::getAttributes() helps answer a focused question: returns PHP attributes attached specifically to the parameter.

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

What Is ReflectionParameter::getAttributes()?


Returns PHP attributes attached specifically to the parameter.

Its method signature is:

public function getAttributes(?string $name = ..., int $flags = ...): array

Parameters:

  • $name — Name of the method, case, property, constant, extension, or parameter being requested May be omitted.
  • $flags — Filtering behavior such as ReflectionAttribute::IS_INSTANCEOF May be omitted.

Return value: array

Example 1: Inspect a formal parameter


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_PARAMETER)]
class Validated { public function __construct(public string $rule) {} }
function submitOrder(#[Validated('positive')] int $quantity, string $currency = 'USD', ?string $note = null): void {}
function updateCounter(int &$total): void { $total++; }
function collectNames(string ...$names): void {}
class OrderRequest { public function __construct(public readonly int $identifier, public string $label = 'New') {} }
$functionReflection = new ReflectionFunction("submitOrder");
$parameterReflection = $functionReflection->getParameters()[0];

$result = $parameterReflection->getAttributes();
echo count($result);

Output:

1

Explanation: Returns PHP attributes attached specifically to the parameter.

Example 2: Compare another parameter position


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_PARAMETER)]
class Validated { public function __construct(public string $rule) {} }
function submitOrder(#[Validated('positive')] int $quantity, string $currency = 'USD', ?string $note = null): void {}
function updateCounter(int &$total): void { $total++; }
function collectNames(string ...$names): void {}
class OrderRequest { public function __construct(public readonly int $identifier, public string $label = 'New') {} }
$functionReflection = new ReflectionFunction("submitOrder");
$parameterReflection = $functionReflection->getParameters()[1];

$result = $parameterReflection->getAttributes();
echo count($result);

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

Output:

0
Result type: array

Explanation: Parameter attributes are not the same as method attributes.

Practical Example: Check a signature before building a call


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_PARAMETER)]
class Validated { public function __construct(public string $rule) {} }
function submitOrder(#[Validated('positive')] int $quantity, string $currency = 'USD', ?string $note = null): void {}
function updateCounter(int &$total): void { $total++; }
function collectNames(string ...$names): void {}
class OrderRequest { public function __construct(public readonly int $identifier, public string $label = 'New') {} }
$functionReflection = new ReflectionFunction("submitOrder");
$parameterReflection = $functionReflection->getParameters()[2];

$result = $parameterReflection->getAttributes();
echo count($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:

0
Entries: 0

Explanation: ReflectionFunctionAbstract::getAttributes reads attributes on the function.

Important Behavior and Edge Cases


  • Parameter attributes are not the same as method attributes.
  • ReflectionFunctionAbstract::getAttributes reads attributes on the function.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionParameter::getAttributes()Returns PHP attributes attached specifically to the parameter.
Related APIReflectionFunctionAbstract::getAttributes reads attributes on the function.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: Parameter attributes are not the same as method attributes.Handle the specific limitation: Parameter attributes are not the same as method attributes.
Confusing this method with Related APIReflectionFunctionAbstract::getAttributes reads attributes on the function.

Key Takeaways


  • Returns PHP attributes attached specifically to the parameter.
  • Parameter attributes are not the same as method attributes.
  • ReflectionFunctionAbstract::getAttributes reads attributes on the function.

Use ReflectionParameter::getAttributes() 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.