
![]() |
perkiset
This thread is a split from another but deserves it's own.
quote author=nutballs link=topic=760.msg5304#msg5304 date=1202834043 so then, how do you explicitly destroy an object? So that it will run the destructor? personally I dont like relying on magical-garbage-collection. Just because I am desperate to not start any actual work this morning, I wrote a quickie to answer it once and for all: #!/usr/bin/ php<? phpecho "Starting test "; $mainInstance = new testClass('mainInstance'); $GLOBALS['globalInstance'] = new testClass('globalInstance'); $tempInstance = new testClass('testInstance'); echo "Unsetting temp instance "; unset($tempInstance); echo "Calling test func "; testFunc(); echo "Back from testfunc "; echo "Overwriting main "; $mainInstance = null; echo "All done "; function testFunc() { $tfInstance = new testClass('testFuncInstance'); } class testClass { protected $instanceName; function __construct($myName) { $this->instanceName = $myName; echo "Constructing {$this->instanceName} "; } function __destruct() { echo " ![]() } } ?> Got these results:
nutballs
cool. I also took it a step further and unset() calls the destructor as well. Plus it also works when you unset a reference to the object it seems.
$GLOBALS[$myName] = &$this; <?
phpecho "Starting test "; $mainInstance = new testClass('mainInstance'); $GLOBALS['globalInstance'] = new testClass('globalInstance'); $tempInstance = new testClass('testInstance'); echo "Unsetting temp instance "; unset($tempInstance); echo "Calling test func "; testFunc(); echo "Back from testfunc "; echo "Overwriting main "; $mainInstance = null; echo "begin testing aaa "; $aaa = new testClass('aaa'); unset($GLOBALS['aaa']); echo "finished testing aaa "; echo "All done "; function testFunc() { $tfInstance = new testClass('testFuncInstance'); } class testClass { protected $instanceName; function __construct($myName) { $this->instanceName = $myName; echo "Constructing {$this->instanceName} "; $GLOBALS[$myName] = &$this; } function __destruct() { echo " ![]() } } ?> perkiset
I wanted to make sure we were seeing what we think: here is proof that
PHPis keeping an instance count and destructing when the last instance is gone...#!/usr/bin/ php<? phpecho "Creating... "; $mainInstance = new testClass('mainInstance'); $anotherInstance = &$mainInstance; $GLOBALS['globalInstance'] = &$mainInstance; echo "unsetting main & another... "; unset($mainInstance); unset($anotherInstance); echo "unsetting global... "; unset($GLOBALS['globalInstance']); echo "All Done "; class testClass { protected $instanceName; function __construct($myName) { $this->instanceName = $myName; echo "Constructing {$this->instanceName} "; } function __destruct() { echo " ![]() } } ?>
|

Thread Categories

![]() |
![]() |
Best of The Cache Home |
![]() |
![]() |
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads