<?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.get-class.php',
    1 => 'get_class',
    2 => 'Returns the name of the class of an object',
  ),
  'up' => 
  array (
    0 => 'ref.classobj.php',
    1 => 'Classes/Object Functions',
  ),
  'prev' => 
  array (
    0 => 'function.get-called-class.php',
    1 => 'get_called_class',
  ),
  'next' => 
  array (
    0 => 'function.get-class-methods.php',
    1 => 'get_class_methods',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/classobj/functions/get-class.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.get-class" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">get_class</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">get_class</span> &mdash; <span class="dc-title">Returns the name of the class of an object</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.get-class-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>get_class</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.object.php" class="type object">object</a></span> <code class="parameter">$object</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   Gets the name of the class of the given <code class="parameter">object</code>.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.get-class-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">object</code></dt>
     <dd>
      <p class="para">
       The tested object.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <span class="simpara">
        Explicitly passing <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> as the <code class="parameter">object</code> is no
        longer allowed as of PHP 7.2.0 and emits an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
        As of PHP 8.0.0, a <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> is emitted when
        <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> is used.
       </span>
      </p></blockquote>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.get-class-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the name of the class of which <code class="parameter">object</code> is an
   instance.
  </p>
  <p class="para">
   If the <code class="parameter">object</code> is an instance of a class which exists 
   in a namespace, the qualified namespaced name of that class is returned.
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.get-class-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   If <span class="function"><strong>get_class()</strong></span> is called with anything other than an
   object, <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> is raised. Prior to PHP 8.0.0,
   an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> level error was raised.
  </p>
  <p class="para">
   If <span class="function"><strong>get_class()</strong></span> is called with no arguments from outside a class,
   an <span class="classname"><a href="class.error.php" class="classname">Error</a></span> is thrown. Prior to PHP 8.0.0,
   an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> level error was raised.
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.get-class-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>8.3.0</td>
       <td>
        Calling <span class="function"><strong>get_class()</strong></span> without an argument now emits an
        <strong><code><a href="errorfunc.constants.php#constant.e-deprecated">E_DEPRECATED</a></code></strong> warning;
        previously, calling this function inside a class returned the name of that class.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        Calling this function from outside a class, without any arguments,
        will now throw an <span class="classname"><a href="class.error.php" class="classname">Error</a></span>.
        Previously, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was raised
        and the function returned <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        Prior to this version the default value for <code class="parameter">object</code>
        was <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> and it had the same effect as not passing any value. Now
        <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> has been removed as the default value for <code class="parameter">object</code>,
        and is no longer a valid input.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.get-class-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Using <span class="function"><strong>get_class()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">foo </span><span style="color: #007700">{<br />    function </span><span style="color: #0000BB">name</span><span style="color: #007700">()<br />    {<br />        echo </span><span style="color: #DD0000">"My name is " </span><span style="color: #007700">, </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">) , </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #FF8000">// create an object<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= new </span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// external call<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Its name is " </span><span style="color: #007700">, </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">) , </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// internal call<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</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">Its name is foo
My name is foo</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 Using <span class="function"><strong>get_class()</strong></span> in superclass</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">abstract class </span><span style="color: #0000BB">bar </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">));<br />        </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">());<br />    }<br />}<br /><br />class </span><span style="color: #0000BB">foo </span><span style="color: #007700">extends </span><span style="color: #0000BB">bar </span><span style="color: #007700">{<br />}<br /><br />new </span><span style="color: #0000BB">foo</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">string(3) &quot;foo&quot;
string(3) &quot;bar&quot;</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-3">
    <p><strong>Example #3 Using <span class="function"><strong>get_class()</strong></span> with namespaced classes</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Foo\Bar</span><span style="color: #007700">;<br /><br />class </span><span style="color: #0000BB">Baz </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br /><br />    }<br />}<br /><br /></span><span style="color: #0000BB">$baz </span><span style="color: #007700">= new </span><span style="color: #0000BB">\Foo\Bar\Baz</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$baz</span><span style="color: #007700">));<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">string(11) &quot;Foo\Bar\Baz&quot;</pre>
</div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.get-class-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.get-called-class.php" class="function" rel="rdfs-seeAlso">get_called_class()</a> - The &quot;Late Static Binding&quot; class name</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.gettype.php" class="function" rel="rdfs-seeAlso">gettype()</a> - Get the type of a variable</span></li>
    <li><span class="function"><a href="function.get-debug-type.php" class="function" rel="rdfs-seeAlso">get_debug_type()</a> - Gets the type name of a variable in a way that is suitable for debugging</span></li>
    <li><span class="function"><a href="function.is-subclass-of.php" class="function" rel="rdfs-seeAlso">is_subclass_of()</a> - Checks if the object has this class as one of its parents or implements it</span></li>
   </ul>
  </p>
 </div>

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