<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.classobj.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.is-subclass-of.php',
    1 => 'is_subclass_of',
    2 => 'Checks if the object has this class as one of its parents or implements it',
  ),
  'up' => 
  array (
    0 => 'ref.classobj.php',
    1 => 'Classes/Object Functions',
  ),
  'prev' => 
  array (
    0 => 'function.is-a.php',
    1 => 'is_a',
  ),
  'next' => 
  array (
    0 => 'function.method-exists.php',
    1 => 'method_exists',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/classobj/functions/is-subclass-of.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.is-subclass-of" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">is_subclass_of</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">is_subclass_of</span> &mdash; <span class="dc-title">Checks if the object has this class as one of its parents or implements it</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.is-subclass-of-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>is_subclass_of</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$object_or_class</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$class</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$allow_string</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Checks if the given <code class="parameter">object_or_class</code> has the class
   <code class="parameter">class</code> as one of its parents or implements it.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.is-subclass-of-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">object_or_class</code></dt>
     <dd>
      <p class="para">
       A class name or an object instance. No error is generated if the class does not exist.
      </p>
     </dd>
    
    
     <dt><code class="parameter">class</code></dt>
     <dd>
      <p class="para">
       The class name
      </p>
     </dd>
    
    
     <dt><code class="parameter">allow_string</code></dt>
     <dd>
      <p class="para">
       If this parameter set to false, string class name as <code class="parameter">object_or_class</code>
       is not allowed. This also prevents from calling autoloader if the class doesn&#039;t exist.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.is-subclass-of-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   This function returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if the object <code class="parameter">object_or_class</code>,
   belongs to a class which is a subclass of
   <code class="parameter">class</code>, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> otherwise.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.is-subclass-of-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>is_subclass_of()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// define a class<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">WidgetFactory<br /></span><span style="color: #007700">{<br />  var </span><span style="color: #0000BB">$oink </span><span style="color: #007700">= </span><span style="color: #DD0000">'moo'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// define a child class<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">WidgetFactory_Child </span><span style="color: #007700">extends </span><span style="color: #0000BB">WidgetFactory<br /></span><span style="color: #007700">{<br />  var </span><span style="color: #0000BB">$oink </span><span style="color: #007700">= </span><span style="color: #DD0000">'oink'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// create a new object<br /></span><span style="color: #0000BB">$WF </span><span style="color: #007700">= new </span><span style="color: #0000BB">WidgetFactory</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$WFC </span><span style="color: #007700">= new </span><span style="color: #0000BB">WidgetFactory_Child</span><span style="color: #007700">();<br /><br />if (</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$WFC</span><span style="color: #007700">, </span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">)) {<br />  echo </span><span style="color: #DD0000">"yes, \$WFC is a subclass of WidgetFactory\n"</span><span style="color: #007700">;<br />} else {<br />  echo </span><span style="color: #DD0000">"no, \$WFC is not a subclass of WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /><br /><br />if (</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$WF</span><span style="color: #007700">, </span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">)) {<br />  echo </span><span style="color: #DD0000">"yes, \$WF is a subclass of WidgetFactory\n"</span><span style="color: #007700">;<br />} else {<br />  echo </span><span style="color: #DD0000">"no, \$WF is not a subclass of WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /><br /><br />if (</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #DD0000">'WidgetFactory_Child'</span><span style="color: #007700">, </span><span style="color: #DD0000">'WidgetFactory'</span><span style="color: #007700">)) {<br />  echo </span><span style="color: #DD0000">"yes, WidgetFactory_Child is a subclass of WidgetFactory\n"</span><span style="color: #007700">;<br />} else {<br />  echo </span><span style="color: #DD0000">"no, WidgetFactory_Child is not a subclass of WidgetFactory\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="function"><strong>is_subclass_of()</strong></span> using interface example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Define the Interface<br /></span><span style="color: #007700">interface </span><span style="color: #0000BB">MyInterface<br /></span><span style="color: #007700">{<br />  public function </span><span style="color: #0000BB">MyFunction</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #FF8000">// Define the class implementation of the interface<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyClass </span><span style="color: #007700">implements </span><span style="color: #0000BB">MyInterface<br /></span><span style="color: #007700">{<br />  public function </span><span style="color: #0000BB">MyFunction</span><span style="color: #007700">()<br />  {<br />    return </span><span style="color: #DD0000">"MyClass Implements MyInterface!"</span><span style="color: #007700">;<br />  }<br />}<br /><br /></span><span style="color: #FF8000">// Instantiate the object<br /></span><span style="color: #0000BB">$my_object </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works since 5.3.7<br /><br />// Test using the object instance of the class<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$my_object</span><span style="color: #007700">, </span><span style="color: #DD0000">'MyInterface'</span><span style="color: #007700">)) {<br />  echo </span><span style="color: #DD0000">"Yes, \$my_object is a subclass of MyInterface\n"</span><span style="color: #007700">;<br />} else {<br />  echo </span><span style="color: #DD0000">"No, \$my_object is not a subclass of MyInterface\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Test using a string of the class name<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">, </span><span style="color: #DD0000">'MyInterface'</span><span style="color: #007700">)) {<br />  echo </span><span style="color: #DD0000">"Yes, MyClass is a subclass of MyInterface\n"</span><span style="color: #007700">;<br />} else {<br />  echo </span><span style="color: #DD0000">"No, MyClass is not a subclass of MyInterface\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Yes, $my_object is a subclass of MyInterface
Yes, MyClass is a subclass of MyInterface</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.is-subclass-of-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
<p class="para">Using this function will use any registered
<a href="language.oop5.autoload.php" class="link">autoloaders</a> if the class is not already known.</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.is-subclass-of-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.get-class.php" class="function" rel="rdfs-seeAlso">get_class()</a> - Returns the name of the class of an object</span></li>
    <li><span class="function"><a href="function.get-parent-class.php" class="function" rel="rdfs-seeAlso">get_parent_class()</a> - Retrieves the parent class name for object or class</span></li>
    <li><span class="function"><a href="function.is-a.php" class="function" rel="rdfs-seeAlso">is_a()</a> - Checks whether the object is of a given type or subtype</span></li>
    <li><span class="function"><a href="function.class-parents.php" class="function" rel="rdfs-seeAlso">class_parents()</a> - Return the parent classes of the given class</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>