php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14539 Assigning null to reference has unexpected results.
Submitted: 2001-12-15 20:15 UTC Modified: 2001-12-27 19:53 UTC
From: jfrisby at mrjoy dot com Assigned:
Status: Not a bug Package: PEAR related
PHP Version: 4.0.6 OS: All
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jfrisby at mrjoy dot com
New email:
PHP Version: OS:

 

 [2001-12-15 20:15 UTC] jfrisby at mrjoy dot com
$foo =& new SomePEARDervidedClassWithADestructor();
$foo = null;

In the above case, the destructor is never called.  Presumably, the assignment is being interpreted as requesting that the object instance be nullified, rather than the more intuitive (IMNSHO) notion of nullifying the reference to the object.

If you change the null-assignment line above to be:
function &nullref() { return null; }
$foo =& nullref();

The desired semantics are preserved.  Of course, the following is presently invalid:
$foo =& null;

May I suggest that the above be added to allow the desired semantics in a simple fashion?  Or at the very least, document this rather unexpected behavior?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-27 07:46 UTC] cox@php.net
References are not C pointers. It's documented in:
http://www.php.net/manual/en/language.references.php

-- Tomas V.V.Cox
 [2001-12-27 16:21 UTC] jfrisby at mrjoy dot com
Frankly this behavior is quite unintuitive and crude.  I didn't expect PHP references to behave like C pointers, but it would be nice if they behaved similarly to references in other major languages such as Java, and Perl.

While the Unix filesystem analogy does clarify things, I think it's a poor model that makes objects and references clumsy to work with.  If you have to explain "X will not do what you expect", you should reconsider whether or not you've chosen the optimal semantics.

And another note:  I would tend to think that objects (and possibly arrays) would best be assigned by reference *by default*, for both reasons of efficiency and expected semantics.

 [2001-12-27 19:53 UTC] ssb@php.net
References and objects in PHP is indeed less-than-intuitive.  This is one of the things adressed in PHP 5.0's object model.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 05:01:27 2024 UTC