title: Get Instance ID of an Object in PHP
body: <p>I've learn a while ago on StackOverflow that <a href="https://stackoverflow.com/questions/1853622/how-to-get-instance-id-with-php">we can get the "instance ID" of any resource</a>, for instance:</p>
  
  <pre><code>var_dump(intval(curl_init()));  // int(2)
  var_dump(intval(finfo_open())); // int(3)
  var_dump(intval(curl_init()));  // int(4)
  var_dump(intval(finfo_open())); // int(5)
  var_dump(intval(curl_init()));  // int(6)
  </code></pre>
  
  <p>I need something similar but applied to classes:</p>
  
  <pre><code>class foo {
      public function __construct() {
          ob_start();
          var_dump($this); // object(foo)#INSTANCE_ID (0) { }
          echo preg_replace('~.+#(\d+).+~s', '$1', ob_get_clean());
      }
  }
  
  $foo = new foo();  // 1
  $foo2 = new foo(); // 2
  </code></pre>
  
  <p>The above works but I was hoping for a faster solution or, at least, one that didn't involve output buffers. Please note that this won't necessarily be used within the constructor or even inside the class itself!</p>
  
  <p><del><code>spl_object_hash()</code> is not what I'm looking for because the two objects produce identical hashes</del></p>
  
  <p>The question previously contained an incorrect example of <code>spl_object_hash</code> output; ensuring that both objects exist at the same time produces hashes which are subtly different:</p>
  
  <pre><code>var_dump(spl_object_hash($foo));  // 0000000079e5f3b60000000042b31773
  var_dump(spl_object_hash($foo2)); // 0000000079e5f3b50000000042b31773
  </code></pre>
  
  <p>Casting to int like resources doesn't seem to work for objects:</p>
  
  <blockquote>
    <p><strong>Notice:</strong> Object of class foo could not be converted to int.</p>
  </blockquote>
  
  <p><strong>Is there a quick way to grab the same output without using object properties</strong>?</p>
  
  <p>Besides <code>var_dump()</code>, I've discovered by trial and error that <a href="http://php.net/manual/en/function.debug-zval-dump.php" rel="noreferrer"><code>debug_zval_dump()</code></a> also outputs the object instance, unfortunately it also needs output buffering since it doesn't return the result.</p>