PHP Doku:: Checks if the class has been defined - function.class-exists.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenKlassen- und ObjektinformationenKlassen- und Objekt-Funktionenclass_exists

Ein Service von Reinhard Neidl - Webprogrammierung.

Klassen- und Objekt-Funktionen




(PHP 4, PHP 5)

class_existsChecks if the class has been defined


bool class_exists ( string $class_name [, bool $autoload = true ] )

Diese Funktion prüft ob eine bestimmte Klasse definiert wurde.



Der Klassenname. Groß- und Kleinschreibung wird bein Vergleich nicht beachtet.


Gibt an ob __autoload genutzt werden soll. Vorgabewert ist TRUE.


Gibt TRUE zurück falls die Klasse class_name definiert ist, sonst FALSE.


Version Beschreibung
5.0.2 Die Funktion liefert nun nicht mehr TRUE für Interfaces. Nutzen Sie hierfür interface_exists().
5.0.0 Der autoload Parameter wurde hinzugefügt.


Beispiel #1 class_exists() Beispiel

// prüft vor Benutzung ob die gewünschte Klasse definiert ist
if (class_exists('MyClass')) {
$myclass = new MyClass();


Beispiel #2 autoload Parameter Beispiel

function __autoload($class)
$class '.php');

// Prüft ob die includierte Datei die Klasse tatsächlich definiert
if (!class_exists($classfalse)) {
trigger_error("Die Klasse $class kann nicht geladen werden"E_USER_WARNING);

if (
class_exists('MyClass')) {
$myclass = new MyClass();


Siehe auch

9 BenutzerBeiträge:
- Beiträge aktualisieren...
myb at mikebevz dot com
23.09.2010 15:50
I've got a problem, which I can't solve.
Let's assume that class Zend_Registry exists in global namespace.

namespace myNs;

class SomeClass {
   public function MyFunc() {
      class_exists('Zend_Registry'); // return false
      class_exists('\Zend_Registry'); // return false
      class_exists('::Zend_Registry'); // return false

How would I check for a class, which exists outside of myNs?
28.04.2010 13:21
If you recursively load several classes inside an autoload function (or mix manual loading and autoloading), be aware that class_exists() (as well as get_declared_classes()) does not know about classes previously loaded during the *current* autoload invocation.

Apparently, the internal list of declared classes is only updated after the autoload function is completed.
azrael dot com at gmail dot com
12.12.2008 1:14
If spl_autoload_register() had been called, then function will try autoload class if it does not exists.

Use instead
($class_name, get_declared_classes());
29.11.2008 19:27
If you planned to use utf-8 in classes or variables names, remember that locale has to be properly set firstly, e.g.
(LC_ALL, 'ru_RU.UTF-8');
or it turn into errors.
Radek @ cz
6.05.2008 3:43
If you want to combat many class includes effectively, define your own autoloader function and spl_autoload_register() that autoloader.
richard at richard-sumilang dot com
27.03.2008 9:56
[ >= PHP 5.3]

If you are checking if a class exists that is in a specific namespace then you have to pass in the full path to the class:

echo (class_exists("com::richardsumilang::common::MyClass")) ? "Yes" : "No";
1.06.2006 10:42
Like someone else pointed out class_exists() is case-INsensitive.

Using in_array() which is case-sensitive, the following function is a case-sensitive version of class_exists().

function class_exists_sensitive( $classname )
   return (
class_exists( $classname ) && in_array( $classname, get_declared_classes() ) );

6.04.2004 14:04
Just a note that at least PHP 4.3.1 seems to crash under some situations if you call class_exists($foo) where $foo is an array (that is, the calling code is incorrect but the error recovery is far from perfect).
anonymous at somewhere dot tld
17.07.2003 21:20
If you have a directory of classes you want to create. (Modules in my instance)... you can do it like that

if (is_dir($this->MODULE_PATH) && $dh = opendir($this->MODULE_PATH)) {
   while ((
$file = readdir($dh)) !== false) {       
      if (
preg_match("/(Mod[a-zA-Z0-9]+).php/", $file, $matches)>0) {               
// include and create the class              
$modules[] = new $matches[1]();
} else {

Here the rule is that all modules are on the form
ModModulename.php and that the class has the same name as the file.
The $modules array has all the classes initialized after this code

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)