[RFC] Weak References

php.internals

Hannes Landeholm

15 years ago
Hello, This is the official RFC discussion thread for Weak References and an announcement of the RFC. Please read the RFC before replying: https://wiki.php.net/rfc/weakreferences In a nutshell: The RFC proposes adding a single class called "SplWeakRef" which provides the "Weak Reference" feature. This is the only change. Previous thread was here: http://news.php.net/php.internals/53963 I'm creating this new thread for several reasons. First of all I feel that the RFC is now somewhat complete and an announcement should be formally made with the [RFC] subject in accordance to the voting procedure. Also the other thread has more general discussion on internal reference counting and garbage collection. Please keep this thread on topic - specifically about Weak References and the RFC. If you plan on voting no to this feature, please reply to this thread so we can discuss your concerns. As I've previously stated, I hope that this feature can be released with PHP 5.4 since I need it for my own projects and open source framework. I don't think this is unreasonable since it's such a simple class and also non-controversial. It has been an established feature in many other major OOP languages for years. I will therefore initiate voting as soon as possible (In two weeks). Thanks Etienne Kneuss for creating the first version of the RFC and the patch and others for discussing it. ~Hannes

Adam Harvey

15 years ago
On Jul 18, 2011 9:20 AM, "Hannes Landeholm" <landeholm@gmail.com> wrote:
> In a nutshell: The RFC proposes adding a single class called "SplWeakRef" > which provides the "Weak Reference" feature. This is the only change.
This may just be Monday morning fuzziness, or I've just missed something in the RFC, but is there a reason weak references would be limited solely to objects, rather than any variable that can be dealt with by-reference? Thanks, Adam

Hannes Landeholm

15 years ago
Weak references is an OOP concept. Scalar data types and arrays are not OOP-related so I don't think that would make sense. Variable references is not the same as object references. A variable reference is simply a variable that is connected with another variable (if you set one it will change the other). If two variables have the same object reference they are not connected, if you set one variable the other is still set to the initial object reference. Also, arays and strings have special memory handling - they use lazy copying so it's opaque if two equal arrays/strings refer to the same region of memory. It's unclear what construction/destruction even mean in that sense. An array might actually have been "constructed" by the compiler. A reference to an object instance is a "pointer" but a reference to a variable is not. http://us.php.net/manual/en/language.references.arent.php I hope that illustrates the difference. Do you have any special use case in mind? ~Hannes On 18 July 2011 20:12, Adam Harvey <aharvey@php.net> wrote:

Adam Harvey

15 years ago
On Jul 18, 2011 11:51 AM, "Hannes Landeholm" <landeholm@gmail.com> wrote:
> Weak references is an OOP concept. Scalar data types and arrays are not
OOP-related so I don't think that would make sense. Variable references is not the same as object references. A variable reference is simply a variable that is connected with another variable (if you set one it will change the other). If two variables have the same object reference they are not connected, if you set one variable the other is still set to the initial object reference.
> > Also, arays and strings have special memory handling - they use lazy
copying so it's opaque if two equal arrays/strings refer to the same region of memory. It's unclear what construction/destruction even mean in that sense. An array might actually have been "constructed" by the compiler. A reference to an object instance is a "pointer" but a reference to a variable is not.
> > http://us.php.net/manual/en/language.references.arent.php > > I hope that illustrates the difference. > > Do you have any special use case in mind?
Nothing in particular, and I understand the differences in memory and reference handling between objects and other variables within PHP. It just seemed like an inconsistency, and I was curious whether it was a design or technical decision. I guess the answer to that is "yes". :) Thanks, Adam

Etienne Kneuss

15 years ago
Hi, On Mon, Jul 18, 2011 at 20:12, Adam Harvey <aharvey@php.net> wrote:
> On Jul 18, 2011 9:20 AM, "Hannes Landeholm" <landeholm@gmail.com> wrote: >> In a nutshell: The RFC proposes adding a single class called "SplWeakRef" >> which provides the "Weak Reference" feature. This is the only change. > > This may just be Monday morning fuzziness, or I've just missed something in > the RFC, but is there a reason weak references would be limited solely to > objects, rather than any variable that can be dealt with by-reference?
Other kinds of variables does not allow us to define specific hooks like for objects. Implementing weak references (note that in this context reference != php reference, but object reference) for other types would require considerable changes to the engine, which we are trying to avoid here. In this case, for objects, the implementation remains quite simple, because objects allow such introspection already. Last but not least, it also makes little sense to do so from a design perspective, as other types do not provide the handle indirection that objects do.
> > Thanks, > > Adam
-- Etienne Kneuss http://www.colder.ch

Hannes Landeholm

15 years ago
Hello guys, I'd just like to remind you that I plan to initiate voting for this RFC on Monday (less than 2 days) so if you plan on voting no, please reply here asap so we can discuss your concerns. Thank you for your time, ~Hannes

Hannes Landeholm

15 years ago
Hi, I'd like to initiate voting on this RFC today since the minimum period of two weeks has now passed. Can someone with the right wiki powers/responsibilities start a vote and announce the [VOTE] on the mailing list? Thank you, ~Hannes

Stas Malyshev

15 years ago
Hi! On 7/31/11 6:59 PM, Hannes Landeholm wrote:
> Hi, > > I'd like to initiate voting on this RFC today since the minimum period of > two weeks has now passed. Can someone with the right wiki > powers/responsibilities start a vote and announce the [VOTE] on the mailing > list?
I think I'm missing some important point here. Why this can't be implemented as PECL extension? I looked at the patch and saw nothing that requires engine involvement. So why not just create a PECL extension? If people start using it and it gets popular, we can merge it to core as we did with many others.
-- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227

Hannes Landeholm

15 years ago
Arguably the feature is already "popular". I have pointed this out many times before but many other major OO languages provides weak references natively. Read the introduction in the RFC. Pragmatically speaking (as a framework designer) you are unfortunately not so privileged on what PECL/PHP extensions people should use and have loaded or not. Many hosting providers does not give you the option to load your own extensions and does not change the default configuration. So you are really avoiding non standard features when designing generic applications/frameworks that should be usable by anyone. This means that the PECL extension popularity proves nothing. Implementing it as a PECL extension will most likely prevent people from using it. I wouldn't. Finally to fully support Weak References in the future it has too make some small adjustments to the GC so the collection event can actually be caught (see rfc/additional cleanup and __destruct()). That might also be reason to why it should be implemented directly. Etienne Kneuss should be able to answer this better. ~Hannes On 1 August 2011 04:28, Stas Malyshev <smalyshev@sugarcrm.com> wrote: