Edit report at http://bugs.php.net/bug.php?id=52182&edit=1
ID: 52182
Comment by: olamedia at gmail dot com
Reported by: olamedia at gmail dot com
Summary: get_object_by_id and get_object_id
Status: Bogus
Type: Feature/Change Request
Package: Unknown/Other Function
PHP Version: 5.3.2
New Comment:
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);
}
Previous Comments:
------------------------------------------------------------------------
[2010-06-25 13:38:55] 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-25 13:17:14] [email protected]
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 12:35:55] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52182&edit=1