It might not be obvious to some, but whenever you reference a namespace in a string, each backslash must be escaped with another backslash because of how PHP handles strings.
<?php
class_exists('\\Zend_Registry'); // return true
?>
class_exists
(PHP 4, PHP 5)
class_exists — Sprawdza, czy klasa została zdefiniowana
Opis
bool class_exists
( string $nazwa_klasy
[, bool $autoload = true
] )
Funkcja sprawdza, czy podana klasa została zdefiniowana.
Parametry
- nazwa_klasy
-
Nazwa klasy. Wielkość liter w nazwie klasy nie ma znaczenia.
- autoload
-
Czy wywołać domyślnie __autoload.
Zwracane wartości
Zwraca TRUE jeśli nazwa_klasy jest klasą, w przeciwnym przypadku zwraca FALSE.
Rejestr zmian
| Wersja | Opis |
|---|---|
| 5.0.2 | Nie zwraca już TRUE dla zdefiniowanych interfejsów. W przypadku interfesów należy używać interface_exists(). |
| 5.0.0 | Dodano parametr autoload. |
Przykłady
Przykład #1 Przykład class_exists()
<?php
// Sprawdź czy klasa istnieje przed próbą jej użycia
if (class_exists('MyClass')) {
$myclass = new MyClass();
}
?>
Przykład #2 Przykład z parametrem autoload
<?php
function __autoload($class)
{
include($class . '.php');
// Sprawdź czy w dołączanym pliku była deklaracja klasy
if (!class_exists($class, false)) {
trigger_error("Nie można załadować klasy: $class", E_USER_WARNING);
}
}
if (class_exists('MyClass')) {
$myclass = new MyClass();
}
?>
Zobacz też:
- function_exists() - Return TRUE if the given function has been defined
- interface_exists() - Checks if the interface has been defined
- get_declared_classes() - Zwraca tablicę z nazwami zdefiniowanych klas
Anonymous
07-Mar-2011 03:21
myb at mikebevz dot com
23-Sep-2010 12: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?
Klaus
27-Apr-2010 10: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
11-Dec-2008 09: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 03: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
05-May-2008 12: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
26-Mar-2008 06: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
31-May-2006 07: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() ) );
}
?>
05-Apr-2004 11: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 06: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
