php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52182 get_object_by_id and get_object_id
Submitted: 2010-06-25 12:35 UTC Modified: 2010-06-25 13:17 UTC
From: olamedia at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.2 OS:
Private report: No CVE-ID: None
 [2010-06-25 12:35 UTC] olamedia at gmail dot com
Description:
------------
Requesting new functions:
get_object_id() to get identifier of the object like in var_dump() (Object #XXX)
get_object_by_id() to get object by identifier

Related bug in test script


Test script:
---------------
class a{
  protected $b;
  public function __construct($b){
     $this->b = $b;
  }
}
class b{
}
$b = new b()
$a = new a($b);
unset($b); // __destruct will not be called because of link left in $a

Expected result:
----------------
class a{
  protected $b;
  public function __construct($b){
     $this->b = get_object_id($b); // integer
  }
  public function getB(){
    return get_object_by_id($this->b);
  }
}
class b{
}
$b = new b()
$a = new a($b);
var_dump($a->getB()); // Object #
unset($b); // __destruct will be called because no links left
var_dump($a->getB()); // boolean false


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-25 13:17 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-06-25 13:17 UTC] johannes@php.net
Your design looks broken. Why should unset a random pointer to the object destroy it? If you really need to unset them - which I'd most likely consider broken design - add a method to invalidate it this ...

If you really want to do this the needed function is available - spl_object_hash().

But mind that we don't have any true unique identifier - once an object is destroyed it's ID might be re-used by others.
 [2010-06-25 13:38 UTC] olamedia at gmail dot com
2 johannes
So you think, that I need to force call $object->invalidate() / garbage collection 
each time I want no memory leaks? Well, I don't need a "unique id", just "lifetime 
unique id".
spl_object_hash() don't have a reverse function spl_get_object_by_hash()
 [2010-06-26 09:15 UTC] olamedia at gmail dot com
I can describe it better:

I know that when I'm calling $object->property->method(), $object IS STILL 
ALIVE, but I can't access $object in this method() because if I'll hold 
reference to $object in this $property, I'll be required to call GC or destroy 
$object and all properties manually:

foreach ($iterator as $object){
  // do something
  $object->destroy(); // ... foreach ($this->properties as $property) $property-
>destroy();
}
...
function doSomething(){
   $object = new object();
   $object2 = new object();
   $object3 = new object();
   $object4 = new object();
   // do something
   destroy($object, $object2, $object3, $object4);
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 20:01:29 2024 UTC