<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>

CC BY-SA 4.0
2018-07-22T20:41:25.960Z
looks like I forgot `use craft\elements\Asset;` which would have made the original code work. Thanks!
kisonay
CC BY-SA 4.0
2018-07-23T00:50:43.423Z
I think doing it this way is a good bit cleaner
andrew.welch
CC BY-SA 4.0
2018-07-23T08:38:44.040Z
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.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2018-07-23T08:39:37.223Z
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 :)
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2018-07-23T11:36:12.300Z
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.
kisonay
CC BY-SA 4.0
2018-07-23T17:11:00.627Z
`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
andrew.welch
CC BY-SA 4.0
2018-07-22T17:56:12.713Z
2018-07-22T17:56:12.713Z
andrew.welch
<p>Assuming you're using Craft 3, this should work:</p>

<pre><code>if (\get_class($asset) !== 'craft\elements\Asset) {
    return false;
}
</code></pre>

CC BY-SA 4.0
2018-07-22T14:58:14.403Z
2018-07-22T15:20:52.590Z
2018-07-22T15:20:52.590Z
Mats Mikkel Rummelhoff
<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>

<twigextension>
How can I validate that I am passing an Asset into my custom twig filter?
# 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 @MatsMikkelRummelhoff, 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;
}


---

