|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-04-04 19:25 UTC] k at phpkoala dot com
Description:
------------
In PHP4, calling unset($this) within a class worked fine, and destroyed that class instance. This was a very useful way technique, one that I and others have used many times.
In PHP5, it simply no longer works. There is no error message - not even a strict - the instance is just unaffected.
PHP4 also offers another method - setting $this = NULL, but PHP5 gives an error that $this cannot be reassigned.
I would like to see this functionality restored, or, an alternate mechanism for destroying class instances internally should be pointed out (and put into the manual), or, if for some unknown reason this useful functionality is now declared by the PHP staff to be evil, the reasons should be revealed and the appropriate details put into the manual so that we know it know longer works in PHP5, and why.
I figure it's just a bug ;)
Reproduce code:
---------------
class test {
function f1() {
unset($this);
}
function f2() {
$this = NULL;
}
}
$obj = new test;
var_dump($obj);
$obj->f1();
var_dump($obj);
$obj->f2();
var_dump($obj);
unset($obj);
var_dump($obj);
Expected result:
----------------
object(test)(0) {
}
NULL
NULL
NULL
Note: if f1() worked, there would be no need to run f2(), because $obj would have been unset. But, both methods should result in $obj being NULL.
OR:
object(test)(0) {
}
object(test)(0) {
}
NULL
NULL
This would also be an acceptable result, because there is an alternate method (f2()) that can be used. This is the result from the latest version of PHP4.
Actual result:
--------------
From PHP5:
Fatal error: Cannot re-assign $this in /home/test2.php on line 6
So, f2(), which sets $this to NULL, doesn't work. Commenting that out produces:
object(test)#1 (0) {
}
object(test)#1 (0) {
}
NULL
So, neither of the methods f1() or f2() work.
From the latest version of PHP4:
object(test)(0) {
}
object(test)(0) {
}
NULL
NULL
This is fine, as the desired effect can still be accomplished.
In earlier versions of PHP4, both f1() and f2() work fine.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 11:00:01 2025 UTC |
class FalseClass { public function __destruct() { echo "Gonna die, man"; } } $f = new FalseClass; unset($f); RESULTS: Gonna die, manIt's tough to understand what your problem is without more code. If you are doing: for ($i = 0; $i < 1000000; $i++) { $obj = new Foo(); // do stuff with $obj unset($obj); } That should be sufficient. Could you be more specific? Usually, if the PHP interpreter is running out of memory, you've got some bad code that needs refactoring.This is really quite simple to understand folks... The guy has a class that is sitting there doing it's thing. He has a function (say it's 'DestroyMe') that should do simply that. The best case I can imagine... The class is 'object' and it interfaces with a row in a table in a database. If you're going to delete the object's row from the table, it probably makes sense to destroy the class handling it. function destroy() { delete row; destruct class; } Thus it would be helpful to be able to do what he's asking. Now whether it fits within the realm of responsible coding or not I'm not the judge... lol