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

 
<?

php

 

echo "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 "Applauseestructing {$this->instanceName} ";
}
}

?>


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
[/pre]

Think that about does it...

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;


<?

php

 

echo "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 "Applauseestructing {$this->instanceName} ";
}
}

?>

perkiset

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...


#!/usr/bin/

php

 
<?

php

 

echo "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 "Applauseestructing {$this->instanceName} ";
}
}

?>


Creating...
Constructing mainInstance
unsetting main & another...
unsetting global...
Destructing mainInstance
All Done
[/pre]


Perkiset's Place Home   Politics @ Perkiset's