Every Anonymous Class Is Different

no-email (Damien Seguy), Damien Seguy
Mon, 25 Nov 2024 00:00:00 +0000

Anonymous classes don't have an explicit name, yet PHP uses one internally. That name is build on top of the localisation of the class definition in the code.
This means that two anonymous class objects (sic), created with the same code, but at different places in the code, are considered of a different type by PHP.
On the other hand, when the same piece of code produces the anonymous class object, it is considered of the same class.
Note that the syntax ``foo() instanceof $x`` is valid, but ``$x instanceof foo()`` is not. The second operand of ``instanceof`` is a bit precious, and forbid many expressions: this is the case of a simple assignation.