Using require_once() would help against including a class file more than once.
class_exists
(PHP 4, PHP 5)
class_exists — Checks if the class has been defined
Beschreibung
bool class_exists
( string
$class_name
[, bool $autoload = true
] )Diese Funktion prüft ob eine bestimmte Klasse definiert wurde.
Parameter-Liste
-
class_name -
Der Klassenname. Groß- und Kleinschreibung wird bein Vergleich nicht beachtet.
-
autoload -
Gibt an ob __autoload genutzt werden soll. Vorgabewert ist
TRUE.
Rückgabewerte
Gibt TRUE zurück falls die Klasse class_name
definiert ist, sonst FALSE.
Changelog
| 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.
|
Beispiele
Beispiel #1 class_exists() Beispiel
<?php
// prüft vor Benutzung ob die gewünschte Klasse definiert ist
if (class_exists('MyClass')) {
$myclass = new MyClass();
}
?>
Beispiel #2 autoload Parameter Beispiel
<?php
function __autoload($class)
{
include($class . '.php');
// Prüft ob die includierte Datei die Klasse tatsächlich definiert
if (!class_exists($class, false)) {
trigger_error("Die Klasse $class kann nicht geladen werden", E_USER_WARNING);
}
}
if (class_exists('MyClass')) {
$myclass = new MyClass();
}
?>
Siehe auch
- function_exists() - Falls die angegebene Funktion definiert ist, wird TRUE zurück gegeben
- interface_exists() - Prüft ob ein bestimmtes Interface definiert wurde
- get_declared_classes() - Ermittelt die Namen der definierten Klassen
class_exists
peterw
14-May-2007 01:27
14-May-2007 01:27
Frayja
01-Jun-2006 10:42
01-Jun-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().
<?php
function class_exists_sensitive( $classname )
{
return ( class_exists( $classname ) && in_array( $classname, get_declared_classes() ) );
}
?>
josh at digitalfruition dot com
25-Jun-2005 08:23
25-Jun-2005 08:23
Just to note, class names appear to be case INsensitive. (At least at the time of writing in PHP 4.3.9). Take the following example:
<?php
class Foo { var $myVar; }
class Foo_Bar extends Foo { var $myVar2;}
echo class_exists('Foo'); //true
echo class_exists('foo'); //true
echo class_exists('Foo_Bar'); // true
echo get_class_parent('Foo_Bar'); // foo (NOTE: NOT Foo!)
?>
andrey at php dot net
24-Oct-2004 01:43
24-Oct-2004 01:43
In 5.0.0 and 5.0.1 there was no interface_exists() and in these versions class_exists() used to return TRUE for known interfaces. Starting 5.0.2 this is no more.
06-Apr-2004 02: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-Jul-2003 09:20
17-Jul-2003 09:20
If you have a directory of classes you want to create. (Modules in my instance)... you can do it like that
<?php
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
require_once($this->MODULE_PATH."/".$file);
$modules[] = new $matches[1]();
}
}
} else {
exit;
}
?>
//---
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
cristiano at aspatech dot com dot br
24-Jun-2002 08:36
24-Jun-2002 08:36
This can be veeeery usefull if you use classes that uses other classes, which can be used in your front end. In other words, when you lost the control of which classes are declared in which point of the application, that can generate the "Cannot redeclare class". Use like
<?php
if ( !class_exists( "YourClass" ) ) {
class YourClass {
//your code
}
}
?>
Thats it... Resolve all your problems =)
spamless_blair at nb dot net
09-Oct-2001 10:48
09-Oct-2001 10:48
I have a script that includes various class libraries depending on what is contained in the constant _INCLUDE_LIST which is a comma-delimited string.
define('_INCLUDE_LIST', 'CORE, LIB_DATA, LIB_EMAIL');
I use class_exists() to determine if a class definition has been included before creating an instance of it.
if(class_exists('CMySQLConnection')) $oData = new CMySQLConnection;
Hope it is helpful for someone!
-Jason Garber
IonZoft.com
emurano at optushome dot com dot au
09-Mar-2001 09:35
09-Mar-2001 09:35
Multiple inclusion protection can be achieved in php3 and for non class related code, for example:
<?php
if(!defined("_SOME_UNIQUE_CONSTANT_")) {
define("_SOME_UNIQUE_CONSTANT_",TRUE);
// Your protected code here
}
?>
Make sure that you use a different constant for each protected piece of code. Generally this is used on a file per file basis. Just #ifndef in C!