php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70919 class declarations may not be nested - why not!?
Submitted: 2015-11-15 19:34 UTC Modified: 2016-03-27 09:57 UTC
Votes:10
Avg. Score:3.0 ± 1.5
Reproduced:6 of 7 (85.7%)
Same Version:2 (33.3%)
Same OS:2 (33.3%)
From: teo8976 at gmail dot com Assigned:
Status: Suspended Package: Class/Object related
PHP Version: 7.0.0RC7 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: teo8976 at gmail dot com
New email:
PHP Version: OS:

 

 [2015-11-15 19:34 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-15 19:52 UTC] requinix@php.net
-Type: Bug +Type: Feature/Change Request -Package: *General Issues +Package: Class/Object related
 [2015-11-16 18:21 UTC] 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-16 22:28 UTC] 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.
 [2016-03-26 21:47 UTC] krakjoe@php.net
-Status: Open +Status: Suspended
 [2016-03-26 21:47 UTC] krakjoe@php.net
For this kind of change, an RFC is required.

Please see: https://wiki.php.net/rfc/howto
 [2016-03-26 22:05 UTC] teo8976 at gmail dot com
> 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.
 [2016-03-27 06:32 UTC] requinix@php.net
> You may have missed this:
HHVM is not PHP. They are separate entities.
 [2016-03-27 09:39 UTC] teo8976 at gmail dot com
Oh, I see.

Anyway, if you need an RFC, this issue should stay open until somebody writes one. Writing an RFC is part of fixing the issue (or the feature request if you prefer to call it that way).
 [2016-03-27 09:57 UTC] krakjoe@php.net
This is well defined, expected behaviour; There is no issue, or bug.

What you are requesting is a change to the language, and as mentioned, these kinds of changes must go through the RFC process.

Please see: https://github.com/php/php-src/blob/master/CONTRIBUTING.md#feature-requests
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC