PHP class_exists

Created with Sketch.

PHP class_exists

Summary: in this tutorial, you’ll learn how to use the PHP class_exists() function to check if a class exists or not.

Introduction to the PHP class_exists() function

The class_exists() function accepts a class name and returns true if the class exists or false otherwise.

class_exists(string $class, bool $autoload = true): bool

Code language: PHP (php)

The class_exists() function has two parameters:

  • $class specifies the name of the class to check.
  • $autoload determines whether to call spl_autoload_register() by default.

Note that the class_exists() is case-insensitive. It means that if you have a class with the name User, the class_exists(‘user’) will return true.

The class_exists() function is often used in model-view-controller (MVC) frameworks to load the controller class based on a route.

PHP class_exists() function examples

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

1) Using the PHP class_exists() function to check if a class exists

First, define a class User in the User.php file:

<?php

class User
{
}

Code language: PHP (php)

Second, require the User.php file in the index.php and use the class_exists() function to check if the User class exists:

<?php

require 'User.php';

if (class_exists('User')) {
echo 'The class User exists';
} else {
echo 'The class User does not exist';
}

Code language: PHP (php)

Since the require construct loads the User.php class, you’ll see the following output:

The class User exists

Code language: PHP (php)

If you comment the require statement and execute the script again, you’ll see the following output:

The class User does not exist

Code language: PHP (php)

2) Using the PHP class_exists() function to check if a namedspaced clas exists

First, add the App namespace to the User class:

<?php

namespace App;

class User
{
}

Code language: PHP (php)

Second, execute the following script:

<?php

require 'User.php';

if (class_exists('User')) {
echo 'The class User exists';
} else {
echo 'The class User does not exist';
}

Code language: PHP (php)

… you’ll see the following output:

The class User does not exist

Code language: PHP (php)

The reason is that the User class is namespaced. The class name is App\User, not User.

To fix this, you can use the fully-qualified class name like this:

<?php

require 'User.php';

if (class_exists('App\User')) {
echo 'The class App\User exists';
} else {
echo 'The class Appp\User does not exist';
}

Code language: PHP (php)

If you execute the script again, you’ll see this output:

The class App\User exists

Code language: PHP (php)

3) Using the class_exists() with the class alias

The class_exists() doesn’t work with the aliased class name. For example:

require 'User.php';
use App\User as Account;
var_dump(class_exists('Account')); // bool(false)

Code language: PHP (php)

4) Using the PHP class_exists() with spl_autoload_register() example

First, define the User class in the User.php file in the app folder as follows:

<?php

namespace App;

class User
{
public function avartar(): string
{
return 'default';
}
}

Code language: PHP (php)

Second, create an index.php file in the root folder and place the following code:

<?php

spl_autoload_register(function ($class) {
echo 'Loading the class ' . $class . '<br>';
require $class . '.php';
});

class_exists('App\User');
echo 'Create a new user' . '<br>';

$user = new App\User();
echo $user->avartar();

Code language: PHP (php)

Output:

Loading the class App\User
Create a new user
default
{"mode":"full","isActive":false}

Code language: PHP (php)

How it works.

First, the class_exists() function checks whether the App\User class exists and call the spl_autoload_register() function to load the User.php file from the app folder.

Second, create a new instance of the class App\User and call the avartar() method.

Summary

  • Use the PHP class_exists() function to check if a class exists or not.
  • Use the fully-qualified name for the class if the class is namespaced.

Leave a Reply

Your email address will not be published. Required fields are marked *