php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52243 post 5.3 - keyword: delete, "force destruct"
Submitted: 2010-07-04 09:28 UTC Modified: 2010-08-07 01:34 UTC
From: mazdak1 at telia dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.3.2 OS: Windows XP (Irrelevant)
Private report: No CVE-ID: None
 [2010-07-04 09:28 UTC] mazdak1 at telia dot com
Description:
------------
Currently, it is impossible to force destruct/delete/destroy an object or any variable for that matter. Instead, you must unset all references until ref_count is at 0, at which point the variable/object is destructed/removed from memory.

This works is fine as long as you know where all references are, but not if you don't. And this can happen e.g: when you're a lib developer and have to destruct something that the developer might reference several times, or if you're using a Singleton with a "destroy" method.

Therefore, I propose a new language construct called "delete" (or some other fancy name), which deletes the variable immediately with all it's references and calling destructor if object & available.

Having both unset & delete at your disposal would give you great freedom, both are useful for different situations.

Test script:
---------------
See: http://pastebin.com/pGdsed5M

Note: as long as variables are in the same scope, all will be nulled with:
<?php
$a = 1;
$b =& $a;
$c =& $a;

$a = null;
?>
But this does not satisfy situations where references may be spread over many scopes.

"pseudocode" of what delete($x) should do:

$referencing_x = get_references_to($x);
foreach($referencing_x as $ref)
{
   $ref = null;
}

unset($x);

Expected result:
----------------
With "delete" keyword implemented:
----------------------------------
A destructed

Notice:  Undefined variable: a in D:\Programming\UV\public\TEST\delete.php on line 48

NULL
NULL
NULL

Actual result:
--------------
With unset:
-----------
Notice:  Undefined variable: a in D:\Programming\UV\public\TEST\delete.php on line 52

NULL
object(A)#30 (0) {
}
object(A)#30 (0) {
}

A destructed

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-07 01:34 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-08-07 01:34 UTC] johannes@php.net
Adding such a method would give us tons of errors from users destroying their objects and getting fatal Errors somewhere far away.

It is better if you add a custom "invalidate" method to your classes where this is needed and track that ourself to react gracefully.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 07:01:31 2024 UTC