The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 22, 2019, 05:56:25 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: PHP Destructor Call Time  (Read 3320 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: February 12, 2008, 09:45:10 AM »

This thread is a split from another but deserves it's own.

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:

Code:
#!/usr/bin/php
<?php

echo "Starting test\n";
$mainInstance = new testClass('mainInstance');
$GLOBALS['globalInstance'] = new testClass('globalInstance');

$tempInstance = new testClass('testInstance');
echo 
"Unsetting temp instance\n";
unset(
$tempInstance);

echo 
"Calling test func\n";
testFunc();
echo 
"Back from testfunc\n";

echo 
"Overwriting main\n";
$mainInstance null;

echo 
"All done\n";

function 
testFunc()
{
$tfInstance = new testClass('testFuncInstance');
}


class 
testClass
{
protected $instanceName;

function __construct($myName)
{
$this->instanceName $myName;
echo "Constructing {$this->instanceName}\n";
}

function __destruct()
{
echo "Destructing {$this->instanceName}\n";
}
}

?>


Got these results:

Starting test
Constructing mainInstance
Constructing globalInstance
Constructing testInstance
Unsetting temp instance
Destructing testInstance
Calling test func
Constructing testFuncInstance
Destructing testFuncInstance
Back from testfunc
Overwriting main
Destructing mainInstance
All done
Destructing globalInstance


Think that about does it...
« Last Edit: February 12, 2008, 09:54:16 AM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #1 on: February 12, 2008, 10:24:03 AM »

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;


Code:
<?php

echo "Starting test\n";
$mainInstance = new testClass('mainInstance');
$GLOBALS['globalInstance'] = new testClass('globalInstance');

$tempInstance = new testClass('testInstance');
echo 
"Unsetting temp instance\n";
unset(
$tempInstance);

echo 
"Calling test func\n";
testFunc();
echo 
"Back from testfunc\n";

echo 
"Overwriting main\n";
$mainInstance null;

echo 
"begin testing aaa\n";
$aaa = new testClass('aaa');
unset(
$GLOBALS['aaa']);
echo 
"finished testing aaa\n";
echo 
"All done\n";

function 
testFunc()
{
$tfInstance = new testClass('testFuncInstance');
}


class 
testClass
{
protected $instanceName;

function __construct($myName)
{
$this->instanceName $myName;
echo "Constructing {$this->instanceName}\n";
$GLOBALS[$myName] = &$this;
}

function __destruct()
{
echo "Destructing {$this->instanceName}\n";
}
}

?>
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: February 12, 2008, 10:32:52 AM »

I wanted to make sure we were seeing what we think: here is proof that PHP is keeping an instance count and destructing when the last instance is gone...

Code:
#!/usr/bin/php
<?php

echo "Creating...\n";
$mainInstance = new testClass('mainInstance');
$anotherInstance = &$mainInstance;
$GLOBALS['globalInstance'] = &$mainInstance;

echo 
"unsetting main & another...\n";
unset(
$mainInstance);
unset(
$anotherInstance);
echo 
"unsetting global... \n";
unset(
$GLOBALS['globalInstance']);
echo 
"All Done\n";

class 
testClass
{
protected $instanceName;

function __construct($myName)
{
$this->instanceName $myName;
echo "Constructing {$this->instanceName}\n";
}

function __destruct()
{
echo "Destructing {$this->instanceName}\n";
}
}

?>


Creating...
Constructing mainInstance
unsetting main & another...
unsetting global...
Destructing mainInstance
All Done
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!