|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70294 ReflectionException getCode should return a non-zero code
Submitted: 2015-08-18 16:10 UTC Modified: 2021-09-14 13:00 UTC
From: mattsch at gmail dot com Assigned:
Status: Open Package: Reflection related
PHP Version: 5.6.12 OS: Gentoo
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: mattsch at gmail dot com
New email:
PHP Version: OS:


 [2015-08-18 16:10 UTC] mattsch at gmail dot com
When a ReflectionException is thrown for any reflection class, an exception code is not set (i.e. it always returns 0).  This makes it quite difficult to identify what caused the exception.  As a result, I am forced to parse the exception message to determine what went wrong instead of relying on a constant from the getCode method.

Test script:
try { 
$reflectionFunction = new ReflectionFunction('foo'); 
} catch(Exception $e){ 

Expected result:
A non-zero code.

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-18 20:30 UTC] mattsch at gmail dot com
Correction: If you call ReflectionClass on a class that does not exist, getCode returns -1.  If you call ReflectionProperty on a property that doesn't exist, getCode returns 0 so ReflectionException::getCode is still useless in its current state.
 [2021-09-14 13:00 UTC]
I have to agree that using a single exception type with the same
code for all of this is not the best solution, but I think you can
probe in advance for nearly all cases (e.g. in this case you can
check function_exists('foo)) in advance, what appears to be
preferable anyway.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 21 04:01:30 2024 UTC