Answers.Body: <p>Actually, you had your code just right imo:</p>
  
  <pre><code>use craft\elements\Asset;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>if (!$asset instanceof Asset) {
      return false;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-22T20:41:25.960Z
Answers.Text: looks like I forgot `use craft\elements\Asset;` which would have made the original code work. Thanks!
Answers.UserDisplayName: kisonay
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-23T00:50:43.423Z
Answers.Text: I think doing it this way is a good bit cleaner
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-23T08:38:44.040Z
Answers.Text: I actually agree that this is probably the cleaner solution. In comparing `get_class` and `instanceof`, a useful detail to know is that `instanceof` will also return `true` for a class _extending_ the class you're testing against (i.e. your conditional will evaluate to `true` if you pass it an object w/ a class _extending_ Asset, which may or may not be what you want), where `get_class($object) === 'SomeClass'` will only evaluate to `true` if the object actually has that particular class. In this case, I'm fairly positive it doesn't matter and both approaches will work.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-23T08:39:37.223Z
Answers.Text: Also, instead of hard coding the full, namespaced classname, it's also possible to do `use craft\elements\Asset; ... if (\get_class($asset) === Asset::class) { ...` of course :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-23T11:36:12.300Z
Answers.Text: That is a great callout @MatsMikkelRummelhoff, great to know that this will also acknowledge exertions. In light of this, I've mad this the correct answer.
Answers.UserDisplayName: kisonay
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-23T17:11:00.627Z
Answers.Text: `instanceof` is also a language construct, so it'll be a bit more performant (not that it really matters in most cases): https://stackoverflow.com/questions/3017684/what-is-the-difference-between-is-a-and-instanceof
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-22T17:56:12.713Z
Answers.LastActivityDate: 2018-07-22T17:56:12.713Z
Answers.UserDisplayName: andrew.welch
Answers.Body: <p>Assuming you're using Craft 3, this should work:</p>
  
  <pre><code>if (\get_class($asset) !== 'craft\elements\Asset) {
      return false;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-22T14:58:14.403Z
Answers.LastActivityDate: 2018-07-22T15:20:52.590Z
Answers.LastEditDate: 2018-07-22T15:20:52.590Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I'm creating a custom twig filter in Craft 3 and am struggling with a really simple thing.</p>
  
  <pre><code>{{ entry.backgroundImage.one() | filter }}
  </code></pre>
  
  <p>In my filter, I would like to validate that I am being passed an asset.</p>
  
  <pre><code>if (!$asset instanceof Asset)
  {
      return false;
  }
  </code></pre>
  
  <p>What should I be checking the object against? Asset or something different?</p>
  
Tags: <twigextension>
Title: How can I validate that I am passing an Asset into my custom twig filter?
ThreadText: # How can I validate that I am passing an Asset into my custom twig filter?
  
  (Asked by: username_0 on 2018-07-22)
  
  I'm creating a custom twig filter in Craft 3 and am struggling with a really simple thing.
  
  ```
  {{ entry.backgroundImage.one() | filter }}
  
  ```
  
  In my filter, I would like to validate that I am being passed an asset.
  
  ```
  if (!$asset instanceof Asset)
  {
      return false;
  }
  
  ```
  
  What should I be checking the object against? Asset or something different?
  
  **Tags:** twigextension
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-07-22. Score: 3 | Accepted answer
  
  Actually, you had your code just right imo:
  
  ```
  use craft\elements\Asset;
  
  ```
  
  ...
  
  ```
  if (!$asset instanceof Asset) {
      return false;
  }
  
  ```
  
  > **Comments:**
  > **username_0**: looks like I forgot `use craft\elements\Asset;` which would have made the original code work. Thanks!  
  > **username_1**: I think doing it this way is a good bit cleaner  
  > **username_2**: I actually agree that this is probably the cleaner solution. In comparing `get_class` and `instanceof`, a useful detail to know is that `instanceof` will also return `true` for a class _extending_ the class you're testing against (i.e. your conditional will evaluate to `true` if you pass it an object w/ a class _extending_ Asset, which may or may not be what you want), where `get_class($object) === 'SomeClass'` will only evaluate to `true` if the object actually has that particular class. In this case, I'm fairly positive it doesn't matter and both approaches will work.  
  > **username_2**: Also, instead of hard coding the full, namespaced classname, it's also possible to do `use craft\elements\Asset; ... if (\get_class($asset) === Asset::class) { ...` of course :)  
  > **username_0**: That is a great callout @username_2, great to know that this will also acknowledge exertions. In light of this, I've mad this the correct answer.  
  > **username_1**: `instanceof` is also a language construct, so it'll be a bit more performant (not that it really matters in most cases): https://stackoverflow.com/questions/3017684/what-is-the-difference-between-is-a-and-instanceof  
  
  ---
  
  ### Answer by username_2 on 2018-07-22. Score: 2
  
  Assuming you're using Craft 3, this should work:
  
  ```
  if (\get_class($asset) !== 'craft\elements\Asset) {
      return false;
  }
  
  ```
  
  
  ---
  
  