php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70282 class not shown in reflection exception
Submitted: 2015-08-17 11:59 UTC Modified: 2016-03-28 09:56 UTC
Votes:4
Avg. Score:2.5 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:0 (0.0%)
From: info at dynasource dot eu Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.0.0beta3 OS: W7
Private report: No CVE-ID: None
 [2015-08-17 11:59 UTC] info at dynasource dot eu
Description:
------------
when creating a new ReflectionClass object with a class that doesnt exist, this class is not showing in the reflection class exception. This does happen in 5.6.* versions of PHP.

Test script:
---------------
$reflection = new ReflectionClass($class);



Expected result:
----------------
an exception with "$class does not exist"

Actual result:
--------------
an exception with "Class does not exist" (not the variable $class)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-18 09:07 UTC] laruence@php.net
I don't understand what problem here?

"$class" is  substituted as expected
 [2015-08-18 09:07 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2015-08-18 10:15 UTC] info at dynasource dot eu
-Status: Feedback +Status: Open
 [2015-08-18 10:15 UTC] info at dynasource dot eu
not in the attachment I posted
 [2015-08-18 11:59 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-08-18 11:59 UTC] cmb@php.net
In the supplied test script, $class is not defined, so the
exception message is:

| Uncaught ReflectionException: Class  does not exist in [...]

Note the two spaces between "Class" and "does". Actually, the
name of the class is printed between these two spaces, but the
name is an empty string.
 [2015-08-18 14:10 UTC] info at dynasource dot eu
have you seen my attachment? It says it all.

The error message does not give me any clue about the $class variable, which I wanted to reflect, but had a wrong namespace so ReflectionClass couldnt find it.

The error message should give me something like:

ReflectionException:
Class "\mynamespace\wrongfolder\MyClass" does not exist.

Instead, I get this:

ReflectionException:
Class does not exist.

IMHO, not very informative ;)
 [2015-08-18 14:29 UTC] cmb@php.net
-Status: Not a bug +Status: Feedback
 [2015-08-18 14:29 UTC] cmb@php.net
> have you seen my attachment? It says it all.

Um, there is no attachment. Please post the code directly here if
it is short, and otherwise use some other service (e.g. pastebin).
 [2015-08-19 07:20 UTC] info at dynasource dot eu
-Status: Feedback +Status: Assigned
 [2015-08-19 07:20 UTC] info at dynasource dot eu
Strange. Ive got an image uploaded at:

https://cloud.githubusercontent.com/assets/125726/9287889/20ef278a-432d-11e5-9edf-0a8732f339d1.png
 [2015-08-19 07:31 UTC] requinix@php.net
-Status: Assigned +Status: Feedback
 [2015-08-19 07:31 UTC] requinix@php.net
I assume that's in a browser? Please do a View Source, or a right-click Inspect Element (or whatever), and check the raw HTML for that message. You will probably see the doubled space "Class  does not exist" that @cmb mentioned. If so then it means $class was empty.

Consider doing a var_dump($class) just before that line, too.

While I'm here, testing for an empty class name and/or changing the message to "Class '%s' does not exist" could help this particular case.
 [2015-08-19 10:07 UTC] info at dynasource dot eu
-Status: Feedback +Status: Assigned
 [2015-08-19 10:07 UTC] info at dynasource dot eu
correct, it was because of an empty classname.

The reason why it appeared was because of using __CLASS__ within a Closure
 [2015-08-19 17:38 UTC] cmb@php.net
-Type: Bug +Type: Feature/Change Request
 [2015-08-19 17:38 UTC] cmb@php.net
> While I'm here, testing for an empty class name and/or changing
> the message to "Class '%s' does not exist" could help this
> particular case.

I've checked other class related error messages, and it appears
it's customary to have the class name (single-)quoted. As such it
would make sense to change the reflection related error messages
in this regard. However, the other reflection related error
messages would have to be changed as well (properties, functions,
methods etc. should also be quoted), but that would likely break
all 300+ reflection tests, so that's quite some work.

Anyhow, I still don't think the current behavior is a bug,
therefore changing to feature request.
 [2015-08-19 17:39 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 [2015-08-20 06:38 UTC] info at dynasource dot eu
agreed.

But this problem did arise because of the variable __CLASS__ not to be working in a Closure. Is this normal behavior? It breaks backward compatibility.
 [2015-08-20 11:13 UTC] requinix@php.net
Repro?

__CLASS__ only works within an actual class because it's a compile-time value. Not evaluated. If you defined it within a closure within a class, that's fine, but in a closure outside a class and then evaluated within a class won't work.
And I'm seeing that behavior all the way back to 5.4.0. Except for 7.0.0beta2 which had a bug.
 [2016-03-28 07:57 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug -Package: Reflection related +Package: *General Issues
 [2016-03-28 07:57 UTC] krakjoe@php.net
I don't think a feature request was being made. 

There was a bug fixed that stopped __CLASS__ from working in a Closure declared inside a class, that's since been fixed.

__CLASS__ has never worked inside a closure, it has no sensible value; Closures are final, it's only possible semi-truthful value is "Closure", that's not useful. To give it the value of anything else including __FUNCTION__ would not only be false, but would result in {closure}.

So it doesn't make sense as a bug, or a feature request.

The issue is resolved anyway, so closing the bug :)
 [2016-03-28 09:56 UTC] nikic@php.net
@krakjoe: Just to clarify, if you use a closure inside a class, you'll get that class name. Or, more generally, you'll get the name of the class whose scope was bound to the closure. In that it behaves like self::class.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 07:01:32 2024 UTC