array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.class-exists.php', 1 => 'class_exists', 2 => 'Checks if the class has been defined', ), 'up' => array ( 0 => 'ref.classobj.php', 1 => 'Функції Класів чи Об\'єктів', ), 'prev' => array ( 0 => 'function.class-alias.php', 1 => 'class_alias', ), 'next' => array ( 0 => 'function.enum-exists.php', 1 => 'enum_exists', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/classobj/functions/class-exists.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

class_exists

(PHP 4, PHP 5, PHP 7, PHP 8)

class_existsChecks if the class has been defined

Опис

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

This function checks whether or not the given class has been defined.

Параметри

class

The class name. The name is matched in a case-insensitive manner.

autoload

Whether to autoload if not already loaded.

Значення, що повертаються

Returns true if class is a defined class, false otherwise.

Приклади

Приклад #1 class_exists() example

<?php
// Check that the class exists before trying to use it
if (class_exists('MyClass')) {
$myclass = new MyClass();
}

?>

Приклад #2 autoload parameter example

<?php
spl_autoload_register
(function ($class_name) {
include
$class_name . '.php';

// Check to see whether the include declared the class
if (!class_exists($class_name, false)) {
throw new
LogicException("Unable to load class: $class_name");
}
});

if (
class_exists(MyClass::class)) {
$myclass = new MyClass();
}

?>

Прогляньте також