PHP method_exists

Summary: in this tutorial, you’ll learn how to use the PHP method_exists() function to check if a class or an object of a class has a specified method.

Introduction to the PHP method_exists function

The method_exists() function returns true if an object or a class has a specified method. Otherwise, it returns false.

The syntax of the method_exists() function is as follows:

method_exists(object|string $object_or_class, string $method): boolCode language: PHP (php)

The method_exists() has two parameters:

  • $object_or_class is an object or a class in which you want to check if a method exists.
  • $method is a string that represents the method to check.

PHP method_exists function examples

Let’s take some examples of using the method_exists() function.

1) Using the PHP method_exists() function to check if a class has a method

The following example uses the method_exists() function to check if a method exists in the BankAccount class:

<?php

class BankAccount
{
    public function transferTo(BankAccount $other, float $amount)
    {
        // more code
    }
}

$exists = method_exists(BankAccount::class, 'transferTo');
var_dump($exists); // bool(true)

$exists = method_exists(BankAccount::class, 'deposit');
var_dump($exists); // bool(false)Code language: PHP (php)

In this example, the following statement returns true because the transferTo method exists in the BankAccount class:

method_exists(BankAccount::class, 'transferTo');Code language: PHP (php)

However, the following statement returns false because the deposit method doesn’t exist in the BankAccount class:

method_exists(BankAccount::class, 'deposit');Code language: PHP (php)

2) Using the PHP method_exists function to check if an object has a method

The following example creates a new object of the BankAccount and uses the method_exists() function to check the object has a specified method:

<?php

class BankAccount
{
    public function transferTo(BankAccount $other, float $amount)
    {
        // more code
    }
}

$account = new BankAccount();

$exists = method_exists($account, 'transferTo');
var_dump($exists); // bool(true)

$exists = method_exists($account, 'deposit');
var_dump($exists); // bool(false)Code language: PHP (php)

The $account object has the transferTo method, therefore, the following statement returns true:

method_exists($account, 'transferTo');Code language: PHP (php)

On the other hand, the $account object doesn’t have the deposit method. Therefore, the following statement returns false:

method_exists($account, 'deposit');Code language: PHP (php)

3) Using the method_exists function to check if an object has a static method

The method_exists() also returns true if a class has a static method. For example:

<?php

class BankAccount
{
    public function transferTo(BankAccount $other, float $amount)
    {
        // more code
    }

    public static function compare(BankAccount $other): bool
    {
        // implementation
        // ...
        return false;
    }
}

$exists = method_exists(BankAccount::class, 'compare');
var_dump($exists); // bool(true)

$account = new BankAccount();
$exists = method_exists($account, 'compare');
var_dump($exists); // bool(true)Code language: PHP (php)

The BankAccount has the compare static method, so the following statement returns true:

method_exists(BankAccount::class, 'compare');Code language: PHP (php)

The $account is an instance of the BankAccount class that has the compare static method, the following expression also returns true:

$exists = method_exists($account, 'compare');Code language: PHP (php)

PHP method_exists function in MVC frameworks

The method_exists() method is often used in Model-View-Controller (MVC) frameworks to check if a controller class has a certain method before calling it.

For example, suppose that you have the following request URI:

/posts/edit/1Code language: PHP (php)

This URI has three parts: posts, edit, and 1.

  • The posts maps to the PostsController class.
  • The edit maps the edit method of the class.
  • The number 1 is the post id to edit.

The PostsController class will look like the following:

<?php

class PostsController
{
    public function edit(int $id)
    {
        // show the edit post form
    }
}Code language: PHP (php)

And you use the method_exists() function to check whether the edit method exists in the $controller object like this:

<?php
// ...
if(method_exists($controller, $action))
{
    $controller->$action($id);
}Code language: PHP (php)

Summary

  • Use the PHP method_exists() function to check if an object or a class has a specified method.
Did you find this tutorial useful?