|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52300 spl_get_object_by_hash()
Submitted: 2010-07-09 07:41 UTC Modified: 2010-08-07 01:53 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: olamedia at gmail dot com Assigned:
Status: Wont fix Package: SPL related
PHP Version: 5.3.3RC2 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: olamedia at gmail dot com
New email:
PHP Version: OS:


 [2010-07-09 07:41 UTC] olamedia at gmail dot com
class a{}
$object = new a();
$object2 = spl_get_object_by_hash(spl_object_hash($object));
var_dump($object === $object2); // true (same object)

NOTE: When using spl_get_object_by_hash, you must know that $object must be alive 
else function can return another object.

Primary usage example in test script:

Test script:
class a{
  protected $_b;
  public function getB(){
     return spl_get_object_by_hash($this->_b);
  public function doSomething(){
    $b = $this->getB();
    // here I can use $b !
  public function __construct($b){
     $this->_b = spl_object_hash($b);
class b{
  protected $_a;
  public function getA(){
     return $this->_a;
  public function __construct(){
     $this->_a = new a($this);
  public function __destruct(){
    echo 'destroyed';
$b = new b();
unset($b); // destroyed  !! this IS NOT POSSIBLE without spl_get_object_by_hash()


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-11 12:26 UTC]
If you want weak references, this seems like a poor substitute. Who would want to store a reference that may yield another object in the future?
 [2010-07-11 13:24 UTC] olamedia at gmail dot com
Of course, the real problem is in garbage collector, which can't handle 
crossreferences. This function is quicker to-do alternative.
 [2010-08-07 01:53 UTC]
-Status: Open +Status: Wont fix
 [2010-08-07 01:53 UTC]
There's no good way to implement this.
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC