downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

get_called_class> <class_alias
Last updated: Fri, 13 Nov 2009

view this page in

class_exists

(PHP 4, PHP 5)

class_existsクラスが定義済みかどうかを確認する

説明

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

この関数は指定したクラスが定義されているかどうかを調べます。

パラメータ

class_name

クラス名。大文字小文字は区別しません。

autoload

デフォルトで __autoload をコールするかしないか。 デフォルトは TRUE です。

返り値

クラス class_name が定義されている場合に TRUE、 それ以外の場合に FALSE を返します。

変更履歴

バージョン 説明
5.0.2 定義済みのインターフェイスに対しては TRUE を返さないようになりました。 インターフェイスについては interface_exists() を使用します。
5.0.0 autoload パラメータが追加されました。

例1 class_exists() の例

<?php
// クラスを使用する前に、それが存在するかどうかを調べます
if (class_exists('MyClass')) {
    
$myclass = new MyClass();
}

?>

例2 autoload パラメータの例

<?php
function __autoload($class)
{
    include(
$class '.php');

    
// クラス宣言を含むかどうか確認する
    
if (!class_exists($classfalse)) {
        
trigger_error("Unable to load class: $class"E_USER_WARNING);
    }
}

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

?>

参考



get_called_class> <class_alias
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
class_exists
azrael dot com at gmail dot com
12-Dec-2008 12:14
If spl_autoload_register() had been called, then function will try autoload class if it does not exists.

Use instead
<?php
in_array
($class_name, get_declared_classes());
?>
Anonymous
29-Nov-2008 06:27
If you planned to use utf-8 in classes or variables names, remember that locale has to be properly set firstly, e.g.
<?php
locale
(LC_ALL, 'ru_RU.UTF-8');
?>
or it turn into errors.
Radek @ cz
06-May-2008 01: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-Mar-2008 08: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 08: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 12: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 07: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

get_called_class> <class_alias
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites