php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77815 New feature request - Introduce 'using' keyword/feature similar to C#
Submitted: 2019-03-28 14:11 UTC Modified: 2019-03-28 17:20 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: piotr dot ziarek at yahoo dot co dot uk Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 7.3.4RC1 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: piotr dot ziarek at yahoo dot co dot uk
New email:
PHP Version: OS:

 

 [2019-03-28 14:11 UTC] piotr dot ziarek at yahoo dot co dot uk
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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-28 17:20 UTC] requinix@php.net
-Status: Open +Status: Suspended -Package: PHP Language Specification +Package: Scripting Engine problem
 [2019-03-28 17:20 UTC] requinix@php.net
For language changes like this, please use the RFC process. https://wiki.php.net/rfc/howto
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC