php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #57201 SplObject[Weak]Reference proposal
Submitted: 2006-08-29 05:03 UTC Modified: 2012-01-27 15:23 UTC
From: wb at pro-net dot co dot uk Assigned: colder (profile)
Status: Closed Package: *General Issues
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: wb at pro-net dot co dot uk
New email:
PHP Version: OS:

 

 [2006-08-29 05:03 UTC] wb at pro-net dot co dot uk
Description:
------------
This is just a request for a new SPL class. Its similar to the  SplObjectStorage class but allows you to provide a base class to reference against.

The reasoning behind this is that you might have a situation where you want/need to assign other objects to a specific object instance without having to amend any existing objects and without the objects needing knowing about it. 

An example could be a generic permission system where you have an object and using this new class you assign a permission reference to it which you can then check at a later time.

Reproduce code:
---------------
Reference implementation and simple example available at...

http://wb.pro-net.co.uk/php/splobjectreference.phps

Expected result:
----------------
N/A

Actual result:
--------------
N/A

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-31 21:30 UTC] helly@php.net
Your weak reference sounds very much like JNI. In our case we could eventually do it but we pretty much do not have something to hook into the garbage collection to get rid of otherwise no longer used objects.

A question from my side is whether you want some template/generic solution here or something else. In your example code you had stdClsss as base class but you must remember that not all PHP classes derive from that class. Actually we have no common base class at all. Which basically brings us back to the template/generic solution.

What we could do is provide a function that creates a class derived from SplObjectStorage that only works on a given class or interface. Would that solve what you want?
 [2006-09-01 05:35 UTC] wb at pro-net dot co dot uk
Pardon my ignorance but I have no idea what you mean when you say JNI.

The main functionality I'm looking for is to simply (from a PHP point of view) be able to link objects to /any/ existing object without having to do or worry about the following:

   1. Amend the existing class declarations.
   2. Having to manually implement some kind of 'garbage collection' when your holding object is the only object left with references to the base objects. As far as I can see this can not be done from within a php script.

Just thinking off the top of my head but this could probably be linked in with the recent __toHash() debate on the internals list. If the __toHash() functionality was available along with an object_hash_exists($hashString) function then the functionality could be implemented from within a php script. You wouldn't have automatic GC but you could then provide a cleanUp() method that you could call to check that the object hashes exist and then act accordingly. Also depending on how the GC works internally you might also be able to provide a register_gc_function($callback) so that the cleanUp() method get called whenever the PHP GC is actioned.

As for the example I posted, I was using stdClsss as a way to ensure that only objects are allowed to be passed to the class methods. Off topic but is there a better way to do this or should I just leave out the type hinting and do an is_object($foo) check within the method?

I have already created a php land class that extends SplObjectStorage that only works on given classes or interfaces, however this does not help with the request in hand, although it is useful in its own right.
 [2012-01-27 15:23 UTC] colder@php.net
Seems that what you are looking for is:

https://github.com/colder/php-weakref
 [2012-01-27 15:23 UTC] colder@php.net
-Status: Open +Status: Closed -Package: SPL +Package: *General Issues -Assigned To: helly +Assigned To: colder
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 19:01:28 2025 UTC