ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ принадлСТности Ρ‚ΠΈΠΏΡƒ

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ instanceof опрСдСляСт, содСрТит Π»ΠΈ PHP-пСрСмСнная экзСмпляр ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΠΎΠ³ΠΎ класса.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° принадлСТности ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° классу ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ instanceof

<?php

class MyClass {}

class
NotMyClass {}

$a = new MyClass();

var_dump($a instanceof MyClass);
var_dump($a instanceof NotMyClass);

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

bool(true)
bool(false)

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ instanceof Ρ‚Π°ΠΊΠΆΠ΅ опрСдСляСт, ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚ Π»ΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Ρ…Ρ€Π°Π½ΠΈΡ‚ пСрСмСнная, классу-наслСднику:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° принадлСТности ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° класса-наслСдника супСртипу ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ instanceof

<?php

class ParentClass {}

class
MyClass extends ParentClass {}

$a = new MyClass();

var_dump($a instanceof MyClass);
var_dump($a instanceof ParentClass);

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

bool(true)
bool(true)

Для ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ нСпринадлСТности ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° классу ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ логичСский ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ not.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #3 ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° нСпринадлСТности ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° экзСмпляру класса ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ instanceof с ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ логичСского Π½Π΅

<?php

class MyClass {}

$a = new MyClass();
var_dump(!($a instanceof stdClass));

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

bool(true)

НаконСц, ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ instanceof Ρ‚Π°ΠΊΠΆΠ΅ провСряСт, Ρ€Π΅Π°Π»ΠΈΠ·ΡƒΠ΅Ρ‚ Π»ΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ интСрфСйс:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #4 ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° принадлСТности Ρ‚ΠΈΠΏΠ° ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° интСрфСйсу ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ instanceof

<?php

interface MyInterface {}

class
MyClass implements MyInterface {}

$a = new MyClass();

var_dump($a instanceof MyClass);
var_dump($a instanceof MyInterface);

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

bool(true)
bool(true)

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ instanceof Ρ‡Π°Ρ‰Π΅ ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ с Π±ΡƒΠΊΠ²Π°Π»ΡŒΠ½Ρ‹ΠΌ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ΠΌ класса, Π½ΠΎ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ Ρ‚Π°ΠΊΠΆΠ΅ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ с ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° ΠΈΠ»ΠΈ строковой ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #5 ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° принадлСТности ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° Ρ‚ΠΈΠΏΡƒ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ instanceof с Π΄Ρ€ΡƒΠ³ΠΈΠΌΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹ΠΌΠΈ

<?php

interface MyInterface {}

class
MyClass implements MyInterface {}

$a = new MyClass();
$b = new MyClass();
$c = 'MyClass';
$d = 'NotMyClass';

var_dump($a instanceof $b); // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ $b β€” ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ класса MyClass
var_dump($a instanceof $c); // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ $c β€” строка 'MyClass'
var_dump($a instanceof $d); // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ $d β€” строка 'NotMyClass'

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

bool(true)
bool(true)
bool(false)

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ instanceof Π½Π΅ выбрасываСт ошибок, Ссли провСряСмая пСрСмСнная β€” Π½Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚, ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π²Π΅Ρ€Π½Ρ‘Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ false. ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° принадлСТности констант, ΠΎΠ΄Π½Π°ΠΊΠΎ, Π΄ΠΎ PHP 7.3.0 выбрасывала Ρ„Π°Ρ‚Π°Π»ΡŒΠ½ΡƒΡŽ ΠΎΡˆΠΈΠ±ΠΊΡƒ.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #6 ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° принадлСТности классу ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… Π΄Ρ€ΡƒΠ³ΠΈΡ… Ρ‚ΠΈΠΏΠΎΠ² ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ instanceof

<?php

$a
= 1;
$b = NULL;
$c = fopen('/tmp/', 'r');

var_dump($a instanceof stdClass); // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ $a β€” Ρ†Π΅Π»ΠΎΠ΅ Ρ‚ΠΈΠΏΠ° integer
var_dump($b instanceof stdClass); // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ $b β€” NULL
var_dump($c instanceof stdClass); // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ $c β€” Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ с Ρ‚ΠΈΠΏΠΎΠΌ resource
var_dump(FALSE instanceof stdClass);

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

bool(false)
bool(false)
bool(false)
PHP Fatal error:  instanceof expects an object instance, constant given

Начиная с PHP 7.3.0 константы Π² Π»Π΅Π²ΠΎΠΉ части ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π° instanceof стали допустимыми.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #7 ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° принадлСТности Ρ‚ΠΈΠΏΠ° константы классу ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠΌ instanceof

<?php

var_dump
(FALSE instanceof stdClass);

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π² PHP 7.3:

bool(false)

Начиная с PHP 8.0.0 ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ instanceof Ρ€Π°Π·Ρ€Π΅ΡˆΠΈΠ»ΠΈ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Ρ‚ΡŒ с ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½Ρ‹ΠΌΠΈ выраТСниями. ВрСбования ΠΊ Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ: записываСтся Π² ΠΊΡ€ΡƒΠ³Π»Ρ‹Ρ… скобках ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ с Ρ‚ΠΈΠΏΠΎΠΌ string.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #8 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Ρ€Π°Π±ΠΎΡ‚Ρ‹ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π° instanceof с ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½Ρ‹ΠΌ Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ

<?php

class ClassA extends \stdClass {}
class
ClassB extends \stdClass {}
class
ClassC extends ClassB {}
class
ClassD extends ClassA {}

function
getSomeClass(): string
{
return
ClassA::class;
}

var_dump(new ClassA instanceof ('std' . 'Class'));
var_dump(new ClassB instanceof ('Class' . 'B'));
var_dump(new ClassC instanceof ('Class' . 'A'));
var_dump(new ClassD instanceof (getSomeClass()));

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π² PHP 8:

bool(true)
bool(true)
bool(false)
bool(true)

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ instanceof Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π΅Π½ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ is_a().

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 16 notes

up
142
jphaas at gmail dot com ΒΆ
18 years ago
Posting this so the word typeof appears on this page, so that this page will show up when you google 'php typeof'.  ...yeah, former Java user.
up
64
knarlin at yahoo dot com dot au ΒΆ
13 years ago
Checking an object is not an instance of a class, example #3 uses extraneous parentheses.

<?php
var_dump(!($a instanceof stdClass));
?>

Because instanceof has higher operator precedence than ! you can just do

<?php
var_dump( ! $a instanceof stdClass );
?>
up
50
Sudarshan Wadkar ΒΆ
14 years ago
I don't see any mention of "namespaces" on this page so I thought I would chime in. The instanceof operator takes FQCN as second operator when you pass it as string and not a simple class name. It will not resolve it even if you have a `use MyNamespace\Bar;` at the top level. Here is what I am trying to say:

## testinclude.php ##
<?php
namespace Bar1;
{
class Foo1{ }
}
namespace Bar2;
{
class Foo2{ }
}
?>
## test.php ##
<?php
include('testinclude.php');
use Bar1\Foo1 as Foo;
$foo1 = new Foo(); $className = 'Bar1\Foo1';
var_dump($foo1 instanceof Bar1\Foo1);
var_dump($foo1 instanceof $className);
$className = 'Foo';
var_dump($foo1 instanceof $className);
use Bar2\Foo2;
$foo2 = new Foo2(); $className = 'Bar2\Foo2';
var_dump($foo2 instanceof Bar2\Foo2);
var_dump($foo2 instanceof $className);
$className = 'Foo2';
var_dump($foo2 instanceof $className);
?>
## stdout ##
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
up
6
wapinet at mail dot ru ΒΆ
5 years ago
if you have only class names (not objects) you can use that snippet: https://3v4l.org/mUKUC
<?php
interface i{}
class a implements i{}

var_dump(a::class instanceof i); // false
var_dump(in_array(i::class, class_implements(a::class), true)); // true
up
48
dava ΒΆ
12 years ago
You are also able to compare 2 objects using instanceOf. In that case, instanceOf will compare the types of both objects. That is sometimes very useful:

<?php

class A { }
class B { }

$a = new A;
$b = new B;
$a2 = new A;

echo $a instanceOf $a; // true
echo $a instanceOf $b; // false
echo $a instanceOf $a2; // true

?>
up
8
wadih at creationmw dot com ΒΆ
8 years ago
Doing $a instanceof stdClass from inside a namespace will not work on its own. 

You will have to do:

<?php
if ($a instanceof \stdClass) 
?>
up
9
kevin dot benton at beatport dot com ΒΆ
17 years ago
Example #5 could also be extended to include...

var_dump($a instanceof MyInterface);

The new result would be

bool(true)

So - instanceof is smart enough to know that a class that implements an interface is an instance of the interface, not just the class.  I didn't see that point made clearly enough in the explanation at the top.
up
6
Hayley Watson ΒΆ
8 years ago
If you want to use "$foo instanceof $bar" to determine if two objects are the same class, remember that "instanceof" will also evaluate to true if $foo is an instance of a _subclass_ of $bar's class.

If you really want to see if they are the _same_ class, then they both have to be instances of each other's class. That is:
<?php

($foo instanceof $bar && $bar instanceof $foo)

?>

Consider it an alternative to "get_class($bar) == get_class($foo)" that avoids the detour through to string lookups and comparisons.
up
17
jtaal at eljakim dot nl ΒΆ
17 years ago
You can use "self" to reference to the current class:

<?php
class myclass {
    function mymethod($otherObject) {
        if ($otherObject instanceof self) {
            $otherObject->mymethod(null);
        }
        return 'works!';
    }
}

$a = new myclass();
print $a->mymethod($a);
?>
up
13
fbableus ΒΆ
15 years ago
If you want to test if a classname is an instance of a class, the instanceof operator won't work.

<?php
$classname = 'MyClass';
if( $classname instanceof MyParentClass) echo 'Child of it';
else echo 'Not child of it';
?>

Will always output 
Not child of it

You must use a ReflectionClass :
<?php
$classname = 'MyClass';
$myReflection = new ReflectionClass($classname);
if( $myReflection->isSubclassOf('MyParentClass')) echo  'Child of it';
else echo 'Not child of it';
?>

Will output the good result.
If you're testing an interface, use implementsInterface() instead of isSublassOf().
up
5
wbcarts at juno dot com ΒΆ
14 years ago
SIMPLE, CLEAN, CLEAR use of the instanceof OPERATOR

First, define a couple of simple PHP Objects to work on -- I'll introduce Circle and Point. Here's the class definitions for both:

<?php

class Circle
{
  protected $radius = 1.0;

  /*
   * This function is the reason we are going to use the
   * instanceof operator below.
   */
  public function setRadius($r)
  {
    $this->radius = $r;
  }

  public function __toString()
  {
    return 'Circle [radius=' . $this->radius . ']';
  }
}

class Point
{
  protected $x = 0;
  protected $y = 0;

  /*
   * This function is the reason we are going to use the
   * instanceof operator below.
   */
  public function setLocation($x, $y)
  {
    $this->x = $x;
    $this->y = $y;
  }

  public function __toString()
  {
    return 'Point [x=' . $this->x . ', y=' . $this->y . ']';
  }
}

?>

Now instantiate a few instances of these types. Note, I will put them in an array (collection) so we can iterate through them quickly.

<?php

$myCollection = array(123, 'abc', 'Hello World!',
  new Circle(), new Circle(), new Circle(),
  new Point(), new Point(), new Point());

$i = 0;
foreach($myCollection AS $item)
{
  /*
   * The setRadius() function is written in the Circle class
   * definition above, so make sure $item is an instance of
   * type Circle BEFORE calling it AND to avoid PHP PMS!
   */
  if($item instanceof Circle)
  {
    $item->setRadius($i);
  }

  /*
   * The setLocation() function is written in the Point class
   * definition above, so make sure $item is an instance of 
   * type Point BEFORE calling it AND to stay out of the ER!
   */
  if($item instanceof Point)
  {
    $item->setLocation($i, $i);
  }

  echo '$myCollection[' . $i++ . '] = ' . $item . '<br>';
}

?>

$myCollection[0] = 123
$myCollection[1] = abc
$myCollection[2] = Hello World!
$myCollection[3] = Circle [radius=3]
$myCollection[4] = Circle [radius=4]
$myCollection[5] = Circle [radius=5]
$myCollection[6] = Point [x=6, y=6]
$myCollection[7] = Point [x=7, y=7]
$myCollection[8] = Point [x=8, y=8]
up
2
ASchmidt at Anamera dot net ΒΆ
6 years ago
Using an undefined variable will result in an error.

If variable is in doubt, one must prequalify:

    if ( isset( $MyInstance ) and $MyInstance instanceof MyClass ) ...
up
5
julien plee using g mail dot com ΒΆ
18 years ago
Response to vinyanov at poczta dot onet dot pl:

You mentionned "the instanceof operator will not accept a string as its first operand". However, this behavior is absolutely right and therefore, you're misleading the meaning of an instance.

<?php 'ClassA' instanceof 'ClassB'; ?> means "the class named ClassA is an instance of the class named ClassB". This is a nonsense sentence because when you instanciate a class, you ALWAYS obtain an object. Consequently, you only can ask if an object is an instance of a class.

I believe asking if "a ClassA belongs to a ClassB" (or "a ClassA is a class of (type) ClassB") or even "a ClassA is (also) a ClassB" is more appropriate. But the first is not implemented and the second only works with objects, just like the instanceof operator.

Plus, I just have tested your code and it does absolutely NOT do the same as instanceof (extended to classes)! I can't advise anyone to reuse it. The use of <?php is_instance_of ($instanceOfA, 'ClassB'); ?> raises a warning "include_once(Object id #1.php) …" when using __autoload (trying to look for $instanceOfA as if it was a class name).

Finally, here is a fast (to me) sample function code to verify if an object or class:

<?php
function kind_of (&$object_or_class, $class)
{
    return is_object ($object_or_class) ? 
        $object_or_class instanceof $class
        : (is_subclass_of ($object_or_class $class)
           || strtolower ($object_or_class) == strtolower ($class));
} 
?>
up
2
jeanyves dot terrien at orange-ftgroup dot com ΒΆ
19 years ago
Cross version function even if you are working in php4
(instanceof is an undefined operator for php4)

   function isMemberOf($classename) {
      $ver = floor(phpversion());
      if($ver > 4) {
         $instanceof = create_function ('$obj,$classname','return $obj instanceof $classname;');
         return $instanceof($this,$classname);
      } else {
         // Php4 uses lowercase for classname.
         return is_a($this, strtolower($classname));
      }
   } // end function isMemberOf
up
3
ejohnson82 at gmail dot com ΒΆ
18 years ago
The PHP parser generates a parse error on either of the two lines that are commented out here.  
Apparently the 'instanceof' construct will take a string variable in the second spot, but it will NOT take a string... lame

class Bar {}
$b = new Bar;
$b_class = "Bar";
var_export($b instanceof Bar); // this is ok
var_export($b instanceof $b_class); // this is ok
//var_export($f instanceof "Bar"); // this is syntactically illegal
//var_export($f instanceof 'Bar'); // this is syntactically illegal
up
1
soletan at toxa dot de ΒΆ
19 years ago
Please note: != is a separate operator with separate semantics. Thinking about language grammar it's kind of ridicilous to negate an operator. Of course, it's possible to negate the result of a function (like is_a()), since it isn't negating the function itself or its semantics.

instanceof is a binary operator, and so used in binary terms like this

terma instanceof termb

while ! (negation) is a unary operator and so may be applied to a single term like this

!term

And a term never consists of an operator, only! There is no such construct in any language (please correct me!). However, instanceof doesn't finally support nested terms in every operand position ("terma" or "termb" above) as negation does:

!!!!!!!!!!!!!!term == term

So back again, did you ever write

a !!!!!!!!!!!!= b

to test equivalence?