php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52318 Weak references for PHP
Submitted: 2010-07-12 17:22 UTC Modified: 2012-06-05 23:25 UTC
Votes:25
Avg. Score:4.5 ± 0.8
Reproduced:21 of 23 (91.3%)
Same Version:11 (52.4%)
Same OS:11 (52.4%)
From: brendel at krumedia dot de Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2010-07-12 17:22 UTC] brendel at krumedia dot de
Description:
------------
PHP should introduce weak references like other languages have for example the Vector class of Actionscript 3, WeakReference class of Java or boost's shared_pointer.

This would help PHP CLI programs and other complex PHP scripts. For example, the Doctrine Project implements the Row Data Gateway Design Pattern and therefore use an internal repository (cache of record objects) to guarantee that only one object per table row exists. The cache has no chance to free objects that are no longer in use. So, a free() method is provided that will clean up all relations and remove parts from the repository. But this is no good solution to the problem: freeing will lead to bugs because other code might rely on some of the freed objects but they are no longer intact.

Thanks for reading


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-14 10:15 UTC] brendel at krumedia dot de
(Edit: It's the Dictionary class of Actionscript 3 instead of the Vector class.)
 [2010-07-30 06:09 UTC] 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?
 [2010-07-30 06:33 UTC] 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 09:11 UTC] 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 20:43 UTC] 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-08-03 16:32 UTC] 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-08-03 16:43 UTC] 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-09 08:08 UTC] 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-09 08:21 UTC] 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:57 UTC] 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 10:17 UTC] brendel at krumedia dot de
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.
 [2011-07-11 18:57 UTC] landeholm at gmail dot com
I just ran into this issue again... Please make PHP a modern language and implement this already.
 [2012-06-05 23:25 UTC] nikic@php.net
This is available as a PECL extension: http://www.php.net/manual/en/book.weakref.php
 [2012-06-05 23:25 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 12:01:27 2024 UTC