Req #70919 [Sus]: class declarations may not be nested - why not!?

From: Date: Sat, 26 Mar 2016 22:05:22 +0000
Subject: Req #70919 [Sus]: class declarations may not be nested - why not!?
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200129@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70919&edit=1 ID: 70919 User updated by: teo8976 at gmail dot com Reported by: teo8976 at gmail dot com Summary: class declarations may not be nested - why not!? Status: Suspended Type: Feature/Change Request Package: Class/Object related PHP Version: 7.0.0RC7 Block user comment: N Private report: N New Comment: > For this kind of change, an RFC is required. You may have missed this: > Note: this does work in hhvm. The fix should be backported to all supported versions. Previous Comments: ------------------------------------------------------------------------ [2016-03-26 21:47:43] krakjoe@php.net For this kind of change, an RFC is required. Please see: https://wiki.php.net/rfc/howto ------------------------------------------------------------------------ [2015-11-16 22:28:29] teo8976 at gmail dot com > There's no reason for that Sorter class to > be declared within the function, can be declared outside of it like > a normal class. 1) My code example is stupid, but there are PLENTY of reasons why you may want to declare it inside a function. Of course you can declare it outside, but IT'S NOT THE SAME THING: if it is declared inside the function, the class only exists if and after the function is executed. 2) That possibility has existed since PHP 5.0 3) If you can do it in a "global" function, there's no reason you shouldn't be able to do it in a class method. 3) Note that in hhvm, rather than removing the ability to do it in a function, they have added the ability to do it in a method. That is, they have fixed the bug already. I don't see a reason why the fix shouldn't be backported to non-dead PHP versions. ------------------------------------------------------------------------ [2015-11-16 18:21:25] rahi at atsspec dot co Neither should be valid; way too easy to write messed up code like this...declaring classes within functions, seriously? WTF There's no reason for that Sorter class to be declared within the function, can be declared outside of it like a normal class. ------------------------------------------------------------------------ [2015-11-15 19:34:04] teo8976 at gmail dot com Description: ------------ If this is valid: https://3v4l.org/SUZs2 then so should this: https://3v4l.org/IhSjK Test script: --------------- https://3v4l.org/IhSjK Expected result: ---------------- output: 7 9 5 3 Actual result: -------------- Fatal error: Class declarations may not be nested in /in/IhSjK on line 17 Note: this does work in hhvm. The fix should be backported to all supported versions. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70919&edit=1

« previous php.bugs (#200129) next »