Answers.Body: <p>You can also use the PHP function <a href="http://php.net/manual/en/function.is-a.php" rel="nofollow noreferrer"><code>is_a()</code></a> in addition to the PHP function <a href="http://php.net/manual/en/function.get-class.php" rel="nofollow noreferrer"><code>get_class()</code></a>. </p>
  
  <p>This is a bit different as it also matches classes which have the class as one of its parents, e.g. an element class that extends <code>craft\elements\User</code>.</p>
  
  <p>Example:</p>
  
  <pre><code>if (is_a($element, 'craft\elements\User')) {
      // Do stuff
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>if (is_a($element, craft\elements\User::class)) {
      // Do stuff
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T18:16:49.837Z
Answers.LastActivityDate: 2018-03-12T09:34:18.753Z
Answers.LastEditDate: 2018-03-12T09:34:18.753Z
Answers.UserDisplayName: Jay
Answers.Body: <p>Having arrived here from a Google search while trying to do this in Twig rather than PHP, I thought it might help someone in future to leave an answer, even though it doesn't address your question directly:</p>
  <pre class="lang-twig prettyprint-override"><code>{{ item is instance of('craft\\elements\\Entry') ? 'This element is an entry!' }}
  {{ item is instance of('craft\\elements\\Asset') ? 'This is an asset!' }}
  {{ item is instance of('craft\\elements\\Category') ? 'This is a category!' }}
  {{ item is instance of('craft\\elements\\User') ? 'This is a user!' }}
  {{ item is instance of('craft\\commerce\\elements\\Product') ? '' }}
  </code></pre>
  <p>I find this especially useful in, for example, internal keyword search results listings templates, where the elements that were found might be of different types and you want to take different actions depending on the types.</p>
  <p>Edit 2024 to add: You can also fetch the element type a little more succinctly like this, <em>however</em> (and it's a big HOWEVER!), beware that this requires a database query - so if you're inside a loop, which is probably quite likely given the use case for this, it's going to give you a horrible N+1 problem which will slow down your template:</p>
  <pre class="lang-twig prettyprint-override"><code>{# you probably shouldn't do this due to the N+1 problem it'll cause : #}
  
  {{ craft.app.elements.getElementTypeById(item.id) }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-19T10:42:24.893Z
Answers.LastActivityDate: 2024-05-20T08:23:45.707Z
Answers.LastEditDate: 2024-05-20T08:23:45.707Z
Answers.UserDisplayName: James Smith
Answers.Body: <p>There’s no dedicated method on elements, but you can use <a href="http://php.net/manual/en/function.get-class.php" rel="nofollow noreferrer"><code>get_class()</code></a>. </p>
  
  <pre><code>switch (get_class($element)) {
      case Entry::class:
          // it’s an entry
          break;
  
      case Category::class:
          // it’s a category
          break;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-09T20:36:14.940Z
Answers.Text: Just a simple question, would you mind to explain why you use 2 different functions here? I know the behavior of `::class` and `get_class` I would just like to know why you use `get_class(element) == Entry::class` instead of `element::class == Entry::class`
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-12T09:20:01.993Z
Answers.Text: They are both very different, `::class` returns the fully qualified class name for a *class name* and `get_class()` the fully qualified class name for an *instance of a class*.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-07T10:36:52.713Z
Answers.LastActivityDate: 2018-03-12T09:36:53.407Z
Answers.LastEditDate: 2018-03-12T09:36:53.407Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>I just discovered <code>getElementTypeById()</code></p>
  
  <p>This pretty much does the trick: </p>
  
  <pre><code>  public function getElementType($element) {
  
      $elementType = Craft::$app-&gt;getElements()-&gt;getElementTypeById($element-&gt;id);
  
      if (strpos($elementType, 'Category') !== false) {
        return 'category';
      } else if (strpos($elementType, 'Entry') !== false) {
        return 'entry';
      }
  
    }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-20T00:16:39.893Z
Answers.LastActivityDate: 2018-02-20T00:16:39.893Z
Answers.UserDisplayName: Mark Notton
Answers.Body: <p>Example: do nothing if element is not Entry</p>
  <pre><code>if (!$event-&gt;element instanceof \craft\elements\Entry) { return; }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-01T08:47:44.857Z
Answers.LastActivityDate: 2021-02-01T08:47:44.857Z
Answers.UserDisplayName: Nikolay Bronskiy
Answers.Body: <p>This is a trick I always use when I want the &quot;base name&quot; of a class:</p>
  <pre class="lang-php prettyprint-override"><code>// if $element is an instance of \craft\elements\Entry
  $baseName = array_reverse(explode('\\', get_class($element)))[0]; // 'Entry'
  $type = strtolower($baseName); // 'entry'
  </code></pre>
  <p><code>explode('\\', get_class($element))</code> gives you an array like <code>[&quot;Entry&quot;, &quot;elements&quot;, &quot;craft&quot;]</code>, so at position <code>0</code> you will find the class &quot;base name&quot; which we can use as a type string.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-17T09:29:15.093Z
Answers.LastActivityDate: 2024-07-17T09:29:15.093Z
Answers.UserDisplayName: 7ochem
Body: <p>In Craft 3, I've got a service function that I'm passing either an 'entry' or 'category' element into. </p>
  
  <p>I'd like to know how to differentiate between the possible element types; This kinda of works:</p>
  
  <pre><code>public function getElementType($element) {
    if ( isset($element-&gt;group) ) {
      return = 'category';
    } else if (isset($element-&gt;section)) {
      return = 'entry';        
    }
  }
  </code></pre>
  
  <p>However I can't help but feel this is unreliable and hacky. Is there a better way to check an elements elementType?</p>
  
Tags: <plugin-development><craft3><elementtype>
Title: How to check an elements elementType
ThreadText: # How to check an elements elementType
  
  (Asked by: username_0 on 2018-02-20)
  
  In Craft 3, I've got a service function that I'm passing either an 'entry' or 'category' element into.
  
  I'd like to know how to differentiate between the possible element types; This kinda of works:
  
  ```
  public function getElementType($element) {
    if ( isset($element->group) ) {
      return = 'category';
    } else if (isset($element->section)) {
      return = 'entry';        
    }
  }
  
  ```
  
  However I can't help but feel this is unreliable and hacky. Is there a better way to check an elements elementType?
  
  **Tags:** plugin-development, craft3, elementtype
  
  
  ---
  
  ## 6 answers
  
  ### Answer by username_1 on 2018-03-09. Score: 2 | Accepted answer
  
  You can also use the PHP function [`is_a()`](http://php.net/manual/en/function.is-a.php) in addition to the PHP function [`get_class()`](http://php.net/manual/en/function.get-class.php).
  
  This is a bit different as it also matches classes which have the class as one of its parents, e.g. an element class that extends `craft\elements\User`.
  
  Example:
  
  ```
  if (is_a($element, 'craft\elements\User')) {
      // Do stuff
  }
  
  ```
  
  or
  
  ```
  if (is_a($element, craft\elements\User::class)) {
      // Do stuff
  }
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2020-08-19. Score: 7
  
  Having arrived here from a Google search while trying to do this in Twig rather than PHP, I thought it might help someone in future to leave an answer, even though it doesn't address your question directly:
  
  ```twig
  {{ item is instance of('craft\\elements\\Entry') ? 'This element is an entry!' }}
  {{ item is instance of('craft\\elements\\Asset') ? 'This is an asset!' }}
  {{ item is instance of('craft\\elements\\Category') ? 'This is a category!' }}
  {{ item is instance of('craft\\elements\\User') ? 'This is a user!' }}
  {{ item is instance of('craft\\commerce\\elements\\Product') ? '' }}
  
  ```
  
  I find this especially useful in, for example, internal keyword search results listings templates, where the elements that were found might be of different types and you want to take different actions depending on the types.
  
  Edit 2024 to add: You can also fetch the element type a little more succinctly like this, *however* (and it's a big HOWEVER!), beware that this requires a database query - so if you're inside a loop, which is probably quite likely given the use case for this, it's going to give you a horrible N+1 problem which will slow down your template:
  
  ```twig
  {# you probably shouldn't do this due to the N+1 problem it'll cause : #}
  
  {{ craft.app.elements.getElementTypeById(item.id) }}
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2018-03-07. Score: 2
  
  There’s no dedicated method on elements, but you can use [`get_class()`](http://php.net/manual/en/function.get-class.php).
  
  ```
  switch (get_class($element)) {
      case Entry::class:
          // it’s an entry
          break;
  
      case Category::class:
          // it’s a category
          break;
  }
  
  ```
  
  > **Comments:**
  > **username_4**: Just a simple question, would you mind to explain why you use 2 different functions here? I know the behavior of `::class` and `get_class` I would just like to know why you use `get_class(element) == Entry::class` instead of `element::class == Entry::class`  
  > **username_3**: They are both very different, `::class` returns the fully qualified class name for a *class name* and `get_class()` the fully qualified class name for an *instance of a class*.  
  
  ---
  
  ### Answer by username_0 on 2018-02-20. Score: 1
  
  I just discovered `getElementTypeById()`
  
  This pretty much does the trick:
  
  ```
    public function getElementType($element) {
  
      $elementType = Craft::$app->getElements()->getElementTypeById($element->id);
  
      if (strpos($elementType, 'Category') !== false) {
        return 'category';
      } else if (strpos($elementType, 'Entry') !== false) {
        return 'entry';
      }
  
    }
  
  ```
  
  
  ---
  
  ### Answer by username_5 on 2021-02-01. Score: 1
  
  Example: do nothing if element is not Entry
  
  ```
  if (!$event->element instanceof \craft\elements\Entry) { return; }
  
  ```
  
  
  ---
  
  ### Answer by username_6 on 2024-07-17. Score: 1
  
  This is a trick I always use when I want the "base name" of a class:
  
  ```php
  // if $element is an instance of \craft\elements\Entry
  $baseName = array_reverse(explode('\\', get_class($element)))[0]; // 'Entry'
  $type = strtolower($baseName); // 'entry'
  
  ```
  
  `explode('\\', get_class($element))` gives you an array like `["Entry", "elements", "craft"]`, so at position `0` you will find the class "base name" which we can use as a type string.
  
  
  ---
  
  