Content · Web Development · PHP

PHP ReflectionMethod::setAccessible() Method: Usage and Examples

Legacy API for changing reflected method accessibility.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

When building tools that inspect PHP code, ReflectionMethod::setAccessible() helps answer a focused question: legacy API for changing reflected method accessibility.

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

What Is ReflectionMethod::setAccessible()?


Legacy API for changing reflected method accessibility.

Its method signature is:

public function setAccessible(bool $accessible): void

Parameters:

  • $accessible — Argument named accessible used by this ReflectionMethod::setAccessible operation Required.

Return value: void

Starting in PHP 8.1, this legacy visibility-setting call has no effect because reflected members can already be accessed using reflection.

Example 1: Inspect a reflected method


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_METHOD)]
class ActionLabel { public function __construct(public string $label) {} }
interface Action { public function execute(string $message): string; }
abstract class AbstractAction { abstract public function perform(): void; }
class MailAction implements Action {
    public function __construct(public string $name = 'Mail') {}
    #[ActionLabel('send')]
    public function execute(string $message): string { return $this->name . ': ' . $message; }
    protected function verify(): bool { return true; }
    private function internal(): string { return 'secret'; }
    final public function finalize(): string { return 'complete'; }
    public static function version(): int { return 2; }
    public function __destruct() {}
}
class SmsAction extends MailAction { public function execute(string $message): string { return parent::execute($message); } }
class VerifiedAction extends MailAction { protected function verify(): bool { return false; } }

$methodReflection = new ReflectionMethod(MailAction::class, "execute");

$result = $methodReflection->setAccessible(true);
echo "visibility unchanged; reflection invocation is allowed";

Output:

visibility unchanged; reflection invocation is allowed

Explanation: Legacy API for changing reflected method accessibility.

Example 2: Compare an inherited method


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_METHOD)]
class ActionLabel { public function __construct(public string $label) {} }
interface Action { public function execute(string $message): string; }
abstract class AbstractAction { abstract public function perform(): void; }
class MailAction implements Action {
    public function __construct(public string $name = 'Mail') {}
    #[ActionLabel('send')]
    public function execute(string $message): string { return $this->name . ': ' . $message; }
    protected function verify(): bool { return true; }
    private function internal(): string { return 'secret'; }
    final public function finalize(): string { return 'complete'; }
    public static function version(): int { return 2; }
    public function __destruct() {}
}
class SmsAction extends MailAction { public function execute(string $message): string { return parent::execute($message); } }
class VerifiedAction extends MailAction { protected function verify(): bool { return false; } }

$methodReflection = new ReflectionMethod(SmsAction::class, "execute");

$result = $methodReflection->setAccessible(true);
echo "visibility unchanged; reflection invocation is allowed";

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

Output:

visibility unchanged; reflection invocation is allowed
Result type: null

Explanation: Since PHP 8.1, reflected methods can already be invoked regardless of visibility; this call has no effect.

Practical Example: Inspect application method metadata


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_METHOD)]
class ActionLabel { public function __construct(public string $label) {} }
interface Action { public function execute(string $message): string; }
abstract class AbstractAction { abstract public function perform(): void; }
class MailAction implements Action {
    public function __construct(public string $name = 'Mail') {}
    #[ActionLabel('send')]
    public function execute(string $message): string { return $this->name . ': ' . $message; }
    protected function verify(): bool { return true; }
    private function internal(): string { return 'secret'; }
    final public function finalize(): string { return 'complete'; }
    public static function version(): int { return 2; }
    public function __destruct() {}
}
class SmsAction extends MailAction { public function execute(string $message): string { return parent::execute($message); } }
class VerifiedAction extends MailAction { protected function verify(): bool { return false; } }

$methodReflection = new ReflectionMethod(SmsAction::class, "execute");

$result = $methodReflection->setAccessible(true);
echo "visibility unchanged; reflection invocation is allowed";

// 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:

visibility unchanged; reflection invocation is allowed
No value returned

Explanation: ReflectionMethod::invoke performs the reflected call.

Important Behavior and Edge Cases


  • Since PHP 8.1, reflected methods can already be invoked regardless of visibility; this call has no effect.
  • ReflectionMethod::invoke performs the reflected call.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionMethod::setAccessible()Legacy API for changing reflected method accessibility.
Related APIReflectionMethod::invoke performs the reflected call.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: Since PHP 8.1, reflected methods can already be invoked regardless of visibility; this call has no effect.Handle the specific limitation: Since PHP 8.1, reflected methods can already be invoked regardless of visibility; this call has no effect.
Confusing this method with Related APIReflectionMethod::invoke performs the reflected call.

Key Takeaways


  • Legacy API for changing reflected method accessibility.
  • Since PHP 8.1, reflected methods can already be invoked regardless of visibility; this call has no effect.
  • ReflectionMethod::invoke performs the reflected call.

Use ReflectionMethod::setAccessible() 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.