Edit report at http://bugs.php.net/bug.php?id=52318&edit=1

 ID:                 52318
 Comment by:         brendel at krumedia dot de
 Reported by:        brendel at krumedia dot de
 Summary:            Weak references for PHP
 Status:             Open
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 PHP Version:        Irrelevant
 Block user comment: N

 New Comment:

Sample code use case:



<?php

$repository = new SplWeakArrayValues(); // assume it implements

                                      // ArrayAccess and others



$user = $this->userObjectFromSomewhere();

$repository[$user->id] = $user;



// Now two variables hold object references to one user object



$user = null;



// Since $repository is weak, the destructor of the class of $user
should have been called now

// Also count($repository) should equal 0



?>


Previous Comments:
------------------------------------------------------------------------
[2010-08-03 16:32:36] brendel at krumedia dot de

bastard dot internets at gmail dot com, please see the constructor
parameter "weakKeys" of
http://livedocs.adobe.com/flex/3/langref/flash/utils/Dictionary.html to
get an idea. This is not about variable references.

------------------------------------------------------------------------
[2010-07-30 20:43:47] bastard dot internets at gmail dot com

brendel at krumedia dot de - I was confused by your description,
interpreting your example as describing a problem where you still need
access to data within repository objects that were unexpectedly emptied,
while your reference to weak references could mean this is actually
expected and desired behavior.



Which was why I brought up a dual solution to get mostly to that point
by using an already existing feature.  For example:



<?php



class Registry

{

    public static $Collection = array();



        static function &Enlist($obj)

        {

                $obj->id = count(self::$Collection);

                self::$Collection[$obj->id] =& $obj;

                return $obj;

        }



}



// Both $a and Registry::$Collection[$a->id] will show same object

$a =& Registry::Enlist(new stdClass);



// "Global Free"

Registry::$Collection[$a->id] = null;  // or $a = null; for same effect



?>



This should free the object, even though I think it means you still have
a 3-ref_count 'null' valued zval out there.  But, in order to work
consistently for a certain object, it requires you to always assign your
working variables this way everywhere, and set one of them to "= null"
to truly free the object.  Otherwise, unsetting one will leave all
others intact - if that's what you want.



I was thinking you were describing a language enhancement which should
automatically handle all that.  Perhaps something simply like
"unset($this)" instead to enforce virtual global freeing.



Hopefully this isn't a red herring.  I'm more or less new to alternative
reference types, so I may be misunderstanding how you want this to
apply.  As far as I can tell, "Registry" above can be made to somewhat
act like a combination of a "WeakHashMap" and a reference list.  Can you
add a code example to show how your idea can be implemented?

------------------------------------------------------------------------
[2010-07-30 09:11:37] brendel at krumedia dot de

bastard dot internets at gmail dot com, your comment is not related to
weak references. Weak References are a garbage collection mechanism. The
GC is able to free objects though a object might be referenced by some
weak variable/container etc...



Note to your comment:

To alter properties i do not need to return by variable reference
because object references are fine for this (since PHP 5).

------------------------------------------------------------------------
[2010-07-30 06:33:50] bastard dot internets at gmail dot com

Edit to my previous comment.  I'm sure it's obvious, but sentence # 2
should read:



If assigning $obj2 from a function, declare the function to return by
reference and state "$obj2 =& Factory::Spawn('some data');".

------------------------------------------------------------------------
[2010-07-30 06:09:48] bastard dot internets at gmail dot com

brendel at krumedia dot de - If I understand this issue correctly, this
is already solvable I think by by copying the object by reference:
"$obj2 =& $obj1;".  If assigning $obj2 from a function, declare the
function to return by reference: "$obj2 =& Factory::Spawn('some
data');".  You can then alter same object properties through $obj1 or
$obj2, or null both by just nulling one.  But, if you just
"unset($obj1);" rather than nulling it like "$obj1 = null;", $obj2 will
still be intact.



Does this describe the behavior you're looking for?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    http://bugs.php.net/bug.php?id=52318


-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52318&edit=1

Reply via email to