instanceof negation
Which is the correct format for negating instanceof?
if ( ! $a instanceof stdClass)
or
if ( ! ($a instanceof stdClass) )
I've convinced myself the latter is correct way, probably after reading a blog article several years ago, but after some command-line tests, they both seem equivalent. Are they?
Let's read the docs:
The following table lists the operators in order of precedence, with the highest-precedence ones at the top.[...]
Associativity Operators Additional Information
================== =============== ======================
non-associative instanceof types
right ! logical
So instanceof
has higher priority, thus both statements are equivalent. Parenthesis are probably used to make it obvious so you don't need to look it up in the documentation.
Are they equivalent?
Yes, logically they are equivalent. The answer by Álvaro G. Vicario provides more detail why.
Which is the correct format for negating instance of?
Of the two, there is an argument that the following is more readable. But there is no single way. That's the beauty and curse of PHP.
!($a instanceof stdClass)