Req #48311 [Com]: Controllable object disposing
| From: | maju at 10g dot pl | Date: | Thu, 10 Jan 2019 23:15:49 +0000 |
| Subject: | Req #48311 [Com]: Controllable object disposing | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-218892@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=48311&edit=1
ID: 48311
Comment by: maju at 10g dot pl
Reported by: av3ng3r at gmail dot com
Summary: Controllable object disposing
Status: Open
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: *
PHP Version: *
Block user comment: N
Private report: N
New Comment:
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>";
}
}
?>
Previous Comments:
------------------------------------------------------------------------
[2019-01-10 23:06:41] 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.
------------------------------------------------------------------------
[2009-05-18 22:13:11] 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.
------------------------------------------------------------------------
[2009-05-18 12:25:35] 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-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 https://bugs.php.net/bug.php?id=48311&edit=1