<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Checks if the class has been defined</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.class-alias.html">class_alias</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.get-called-class.html">get_called_class</a></div> <div class="up"><a href="ref.classobj.html">Classes/Object Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.class-exists" class="refentry"> <div class="refnamediv"> <h1 class="refname">class_exists</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">class_exists</span> — <span class="dc-title">Checks if the class has been defined</span></p> </div> <div class="refsect1 description" id="refsect1-function.class-exists-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>class_exists</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$class_name</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$autoload</code><span class="initializer"> = true</span></span> ] )</div> <p class="para rdfs-comment"> This function checks whether or not the given class has been defined. </p> </div> <div class="refsect1 parameters" id="refsect1-function.class-exists-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">class_name</code></em></span> <dd> <p class="para"> The class name. The name is matched in a case-insensitive manner. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">autoload</code></em></span> <dd> <p class="para"> Whether or not to call <a href="language.oop5.autoload.html" class="link">__autoload</a> by default. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.class-exists-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> if <em><code class="parameter">class_name</code></em> is a defined class, <strong><code>FALSE</code></strong> otherwise. </p> </div> <div class="refsect1 changelog" id="refsect1-function.class-exists-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.0.2</td> <td> No longer returns <strong><code>TRUE</code></strong> for defined interfaces. Use <span class="function"><a href="function.interface-exists.html" class="function">interface_exists()</a></span>. </td> </tr> <tr> <td>5.0.0</td> <td> The <em><code class="parameter">autoload</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.class-exists-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5045"> <p><strong>Example #1 <span class="function"><strong>class_exists()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Check that the class exists before trying to use it<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$myclass </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-5046"> <p><strong>Example #2 <em><code class="parameter">autoload</code></em> parameter example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</span><span style="color: #0000BB">$class</span><span style="color: #007700">)<br />{<br /> include(</span><span style="color: #0000BB">$class </span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">// Check to see whether the include declared the class<br /> </span><span style="color: #007700">if (!</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$class</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unable to load class: </span><span style="color: #0000BB">$class</span><span style="color: #DD0000">"</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">);<br /> }<br />}<br /><br />if (</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$myclass </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.class-exists-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.function-exists.html" class="function" rel="rdfs-seeAlso">function_exists()</a> - Return TRUE if the given function has been defined</span></li> <li class="member"> <span class="function"><a href="function.interface-exists.html" class="function" rel="rdfs-seeAlso">interface_exists()</a> - Checks if the interface has been defined</span></li> <li class="member"> <span class="function"><a href="function.get-declared-classes.html" class="function" rel="rdfs-seeAlso">get_declared_classes()</a> - Returns an array with the name of the defined classes</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.class-alias.html">class_alias</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.get-called-class.html">get_called_class</a></div> <div class="up"><a href="ref.classobj.html">Classes/Object Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>