ID:               48311
 Comment by:       a at b dot c dot de
 Reported By:      av3ng3r at gmail dot com
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: -
 PHP Version:      6CVS-2009-05-17 (CVS)
 New Comment:

Your example would be handled by

$reader = new StreamReader($stream);
$reader->write('blahblahblah');
$reader = null; // $reader->__destruct() called

Objects and values are destroyed as soon as the last reference to them
goes away. Remove those references, destroy the object.


$start = microtime(true);

class foo{
        private $name;
function __construct($name) {
        $this->name = $name;
}
function __destruct() {
        echo $this->name." Destroyed at
".(microtime(true)-$GLOBALS['start'])." seconds\n";
}

}

$t1 = new foo('t1');
$t2 = new foo('t2');
$t3 = new foo('t3');

echo "Set \$t1 to null at ".(microtime(true)-$start)." seconds\n";
$t1 = null;
echo "Sleep 5 seconds\n";
sleep(5);
echo "Replace \$t2 with \$t3 at ".(microtime(true)-$start)."
seconds\n";
$t2 = $t3;
echo "Sleep 5 seconds\n";
sleep(5);


Previous Comments:
------------------------------------------------------------------------

[2009-05-17 16:50:23] av3ng3r at gmail dot com

Description:
------------
An implementation like C# (
http://www.coderjournal.com/2007/02/proper-use-idisposable/ ) would be
appreciated:

using(StreamReader reader = new StreamReader(stream))
{
   reader.write('blalblabab');
}

The class StreamReader implements an interface IDisposable, by
implementing this interface (creating a function in the class called;
Dispose) the user can use the statement `using` to control the disposal
of the object manually instead of waiting of the garbage collection of
PHP.

This would be usefull for controling database connections, socket
connections, simplexml?, etc.



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=48311&edit=1

Reply via email to