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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-27 07:46 UTC]
References are not C pointers. It's documented in:

-- 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]
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: Mon Mar 04 03:01:30 2024 UTC