Content · Web Development · PHP

PHP method_exists() Function: Usage and Examples

PHP method_exists() Function: Usage and Examples: Checks whether a named method is declared on a class or object, including non-public methods; existence does not guarantee accessibility.

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

The feature illustrated below operates on PHP values, objects, or language metadata.

What Is method_exists in PHP?


Checks whether a named method is declared on a class or object, including non-public methods; existence does not guarantee accessibility.

method_exists($object_or_class, string $method) : bool

Parameters:

  • $object_or_class — Required. The object_or_class argument used to configure this operation; its accepted values follow the declared PHP type.
  • $method — Required. The method name to inspect or invoke.

Return type: bool. A private or protected method can exist even when external code cannot call it.

Example 1: Understanding the Basic Behavior


<?php

class Document { public function save(): void {} }
var_dump(method_exists(Document::class, 'save'));

Output:

bool(true)

Explanation: Checks whether a named method is declared on a class or object, including non-public methods; existence does not guarantee accessibility.

Example 2: Testing Another Case


<?php

class User { protected function validate(): bool { return true; } }
var_dump(method_exists(new User(), 'validate'));

Output:

bool(true)

Explanation: A private or protected method can exist even when external code cannot call it.

Practical Example: Applying the Operation


<?php

class Queue { public function process(): void {} }
var_dump(method_exists(Queue::class, 'missing'));

Output:

bool(false)

Explanation: is_callable() answers a different question about whether a value is callable.

Important Behavior and Edge Cases


  • A private or protected method can exist even when external code cannot call it.
  • is_callable() answers a different question about whether a value is callable.

Comparison With Related Approaches


ApproachWhen to Use It
method_existsChecks whether a named method is declared on a class or object, including non-public methods; existence does not guarantee accessibility.
is_callable()method_exists() sees methods independent of whether caller can invoke them; is_callable() tests invocability.

Common Beginner Mistakes


MistakeCorrect Approach
Calling a private method from outside the class just because method_exists() returned true.A private or protected method can exist even when external code cannot call it.
Choosing is_callable() without checking whether it provides the same result.method_exists() sees methods independent of whether caller can invoke them; is_callable() tests invocability.

Key Takeaways


  • Checks whether a named method is declared on a class or object, including non-public methods; existence does not guarantee accessibility.
  • A private or protected method can exist even when external code cannot call it.
  • is_callable() answers a different question about whether a value is callable.

The examples demonstrate where method_exists fits within php programming. Verify the relevant preconditions whenever you adapt the code to real data.

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