PHP 7 - Comparing Anonymous Class Instances
<p>I have tried this code:</p>

<pre><code>$ac1 = new class {};
$ac2 = new class {};

var_dump($ac1); // object(class@anonymous)#1 (0) {}
var_dump($ac2); // object(class@anonymous)#2 (0) {}
var_dump(new class {}); // object(class@anonymous)#3 (0) {}

var_dump($ac1 == $ac2); // bool(false)
var_dump($ac1 == new class {}); // bool(false)
var_dump($ac2 == new class {}); // bool(false)
</code></pre>

<p>The result of the above comparisons are all <strong>false</strong>.</p>

<p>However, when I declare a function that returns an anonymous class, this is the result:</p>

<pre><code>function anonymous_class() {
    return new class {};
}

$ac1 = anonymous_class();
$ac2 = anonymous_class();

var_dump($ac1); // object(class@anonymous)#1 (0) {}
var_dump($ac2); // object(class@anonymous)#2 (0) {}
var_dump(anonymous_class()); // object(class@anonymous)#3 (0) {}

var_dump($ac1 == $ac2); // bool(true)
var_dump($ac1 == anonymous_class()); // bool(true)
var_dump($ac2 == anonymous_class()); // bool(true)
</code></pre>

<p>All printed <strong>true</strong>.</p>

<p>Now, the question is, how did that happen? Particularly, why did it print <strong>true</strong> for the second context, knowing that each <code>var_dump()</code> of the instances resulted differently?</p>