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

search for in the

get_class_methods> <call_user_method
Last updated: Fri, 22 Aug 2008

view this page in

class_exists

(PHP 4, PHP 5)

class_existsCheca se uma classe foi definida

Descrição

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

Esta função checa se uma dada classe foi definida.

Parâmetros

class_name

O nome da classe. O nome da classe é verificada sem fazer distinção de maiúscula e minúscula.

autoload

Se deve ou não chamar __autoload por padrão. O padrão é TRUE.

Valor Retornado

Retorna TRUE se class_name é uma classe definida, caso contrário retorna FALSE.

Histórico

Versão Descrição
5.0.2 Não retorna TRUE para interfaces definidas. Use interface_exists().
5.0.0 O parâmetro autoload foi adicionado.

Exemplos

Exemplo #1 Exemplo da class_exists()

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

?>

Exemplo #2 Exemplo do parâmetro autoload

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

    
// Check to see whether the include declared the class
    
if (!class_exists($classfalse)) {
        
trigger_error("Unable to load class: $class"E_USER_WARNING);
    }
}

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

?>



get_class_methods> <call_user_method
Last updated: Fri, 22 Aug 2008
 
add a note add a note User Contributed Notes
class_exists
Radek @ cz
06-May-2008 03: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 09: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 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() ) );
}
?>
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
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_class_methods> <call_user_method
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites