How to check if not instance of some class in symfony2

Viewed 90181

I want to execute some functions if entity is member of few classes but not some.

There is a function called instanceof.

But is there something like

if ($entity !instanceof [User,Order,Product])
6 Answers

PHP manual says: http://php.net/manual/en/language.operators.type.php

!($a instanceof stdClass)

This is just a logical and "grammatically" correct written syntax.

!$class instanceof someClass

The suggested syntax above, though, is tricky because we are not specifying which exactly is the scope of the negation: the variable itself or the whole construct of $class instanceof someclass. We will only have to rely on the operator precendence here [Edited, thanks to @Kolyunya].

PHP Operator Precedence

instanceof operator is just before negation then this expression:

!$class instanceof someClass

is just right in PHP and this do that you expect.

Or you can try these

    $cls = [GlobalNameSpace::class,\GlobalNameSpaceWithSlash::class,\Non\Global\Namespace::class];
    if(!in_array(get_class($instance), $cls)){
        //do anything
    }
Related