If you want to combat many class includes effectively, define your own autoloader function and spl_autoload_register() that autoloader.
class_exists
(PHP 4, PHP 5)
class_exists — 检查类是否已定义
说明
bool class_exists
( string $class_name
[, bool $autoload
] )
如果由 class_name 所指的类已经定义,此函数返回 TRUE,否则返回 FALSE。
Example#1 class_exists() 例子
<?php
// Check the class exists before trying to use it
if (class_exists('MyClass')) {
$myclass = new MyClass();
}
?>
class_exists() 默认将会尝试调用 __autoload,如果不想让 class_exists() 调用 __autoload,可以将 autoload 参数设为 FALSE。
Example#2 autoload 参数例子
<?php
function __autoload($class)
{
include($class . '.php');
// Check to see if the include declared the class
if (!class_exists($class, false)) {
trigger_error("Unable to load class: $class", E_USER_WARNING);
}
}
if (class_exists('MyClass')) {
$myclass = new MyClass();
}
?>
Note: autoload 参数是 PHP 5 添加的。
class_exists
Radek @ cz
06-May-2008 09:43
06-May-2008 09:43
richard at richard-sumilang dot com
27-Mar-2008 04:56
27-Mar-2008 04: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";
Frayja
01-Jun-2006 04:42
01-Jun-2006 04: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() ) );
}
?>
06-Apr-2004 08: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
18-Jul-2003 03:20
18-Jul-2003 03: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
25-Jun-2002 02:36
25-Jun-2002 02: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 04:48
09-Oct-2001 04: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
