[PHP-BUG] Req #77815 [NEW]: New feature request - Introduce 'using' keyword/feature similar to C#

[email protected] ("piotr dot ziarek at yahoo dot co dot uk") Thu, 28 Mar 2019 14:11:48 +0000
Newsgroups php.standards
Message-ID <[email protected]>
From:             piotr dot ziarek at yahoo dot co dot uk
Operating system: 
PHP version:      7.3.4RC1
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:New feature request - Introduce 'using' keyword/feature similar to C#

Description:
------------
Currently there is no way in PHP to control the scope/lifetime of a
variable. In C# there is an interface called IDisposable which allows
for explicit disposal by using a 'using' keyword. 

I'm proposing a explicit destructor to be called in PHP in a similar
manner.
'using' should behave like an 'if' statement - it should not require
curly brackets for a single command inside in it, multiple commands
should be wrapped in curly brackets.

Test script:
---------------
<?
using($t=new Div)	//no semicolon since this should be like an 'if'
	echo 'inside';
echo 'outside';
//
//
//---class Div---------------
class Div{
	function __construct(){
		echo '<div>';
	}
	function __destruct(){
		echo '</div>';
	}
}
?>

Expected result:
----------------
if 'using' was implemented, the example code above should output:
"<div>inside</div>outside"

Actual result:
--------------
'using' currently will display an error:
PHP Parse error:  syntax error, unexpected 'echo' (T_ECHO)
//or depending on a semicolon(just to give you you a real failure
reason)
PHP Fatal error:  Uncaught Error: Call to undefined function using()


without 'using' it will say "<div>insideoutside</div>" - last </div>
will be displayed at the very end of script probably when PHP disposes
all variables.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=77815&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=77815&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=77815&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=77815&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=77815&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=77815&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=77815&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=77815&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=77815&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=77815&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=77815&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=77815&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=77815&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=77815&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77815&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=77815&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=77815&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=77815&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77815&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=77815&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=77815&r=mysqlcfg