Req #52318 [Com]: Weak references for PHP

From: Date: Mon, 09 Aug 2010 08:17:09 +0000
Subject: Req #52318 [Com]: Weak references for PHP
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-153274@lists.php.net to get a copy of this message
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:

bastard dot internets at gmail dot com,

for framework developers this would be very useful. It just means, when
the user of the framework no longer use a certain object the framework
also should not longer waste memory. There are no pitfalls for the user.
But the framework is more flexible.


Previous Comments:
------------------------------------------------------------------------
[2010-08-09 08:57:46] bastard dot internets at gmail dot com

brendel at krumedia dot de - Good call:  "$user now contains a new user
object"

When looking at it from a security perspective, I do believe you just
killed all my arguments against.  Not because you've proven it
impossible, but because unless the developer explicitly wants that
functionality, their code will have a thousand holes.

------------------------------------------------------------------------
[2010-08-09 08:21:48] brendel at krumedia dot de

Hi bastard dot internets at gmail dot com,

see how variable references are no solution:

<?php
$repository = new SplWeakArrayValues();

$user = $dbTable->getUserById(1);
$repository[$user->id] = $user;

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

$user = new User();

// $user now contains a new user object and the old one should have
// been removed from the repository. This time i do not assign null
// but another (transient) user object.
// count($repository) should equal 0.

?>

------------------------------------------------------------------------
[2010-08-09 08:08:26] bastard dot internets at gmail dot com

brendel at krumedia dot de - You got me at "Also count($repository)
should equal 0"

As I said earlier, "Registry::$Collection[$a->id] = null;" will set all
variables referring to this object handle to null globally.  However,
count(Registry::$Collection) is still 1.

I'd still argue this is possible using existing language features. 
However, it means juggling so many references, forced implementing
spaghetti code to get a solution, and a single missing "&" symbol
anywhere leading to hours of debugging.  And, I just found myself
needing just such a solution.

So, I'd have to vote for a built-in interface or class like you
describe.

------------------------------------------------------------------------
[2010-08-03 16:43:39] brendel at krumedia dot de

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

?>

------------------------------------------------------------------------
[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.

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


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


Thread (14 messages)

« previous php.bugs (#153274) next »