php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48311 Controllable object disposing
Submitted: 2009-05-17 16:50 UTC Modified: 2021-01-21 13:47 UTC
Votes:12
Avg. Score:4.2 ± 1.2
Reproduced:4 of 8 (50.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: av3ng3r at gmail dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2009-05-17 16:50 UTC] 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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-18 12:25 UTC] a at b dot c dot de
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);
 [2009-05-18 22:13 UTC] av3ng3r at gmail dot com
I know that's a solution to cope with this. However code-wise it will be cleaner to let only live the reader variable of a certain scope.
 [2011-04-08 18:17 UTC] jani@php.net
-Summary: Controlable object diposing +Summary: Controllable object disposing -Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: - +Operating System: * -PHP Version: 6CVS-2009-05-17 (CVS) +PHP Version: *
 [2019-01-10 23:06 UTC] maju at 10g dot pl
It would be nice if we could have a similar 'using(...){..}' keyword which would allow to control the scope of code block and force the disposal of the object/resource.
 [2019-01-10 23:15 UTC] maju at 10g dot pl
Thanks to this feature you could potentially do something like this:

<?

using(new Brace('div')){
  echo 'DIV INNER TEXT';
}
//this would output:<div>DIV INNER TEXT</div>

class Brace{
 private $tag;
 function __construct($tag) {
  $this->tag=$tag;
  echo "<$this->tag>";
 }
 function __destruct() {
  echo "</$this->tag>";
 }
}

?>
 [2021-01-21 13:47 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-01-21 13:47 UTC] cmb@php.net
> However code-wise it will be cleaner to let only live the reader
> variable of a certain scope.

You can use function scope:

    (function () {
        $reader = XMLReader::XML('<root/>');
        var_dump($reader->read());
    })();

Given that this only slightly more verbose than the suggested
using statement, and given that that feature likely would be
implemented like the above alternative, I don't see much value in
it.  If anybody feels strongly about needing the feature, please
pursue the RFC process[1].  For the time being, I'm suspending
this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 02:01:29 2024 UTC