php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53202 unexpected return value getConstant
Submitted: 2010-10-29 15:51 UTC Modified: 2010-10-29 18:49 UTC
From: agent_harris at secure-mail dot biz Assigned:
Status: Wont fix Package: Reflection related
PHP Version: 5.3.3 OS: Ubuntu Linux 9.04
Private report: No CVE-ID: None
 [2010-10-29 15:51 UTC] agent_harris at secure-mail dot biz
Description:
------------
to make it short:

if you try to get the value of a constant via ReflectionClass::getConstant(...) and the constant name you specified does not exist, the function returns bool(false).

but maybe you have really defined a constant as bool(false). then you are not able to distinguish between these two possibilities.

in that case the preferred behavior should be to throw and exception or return the null value. 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-29 17:17 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 [2010-10-29 17:17 UTC] rasmus@php.net
I don't see how returning NULL helps here.  You can just as easily have defined 
the constant to be NULL.  There is no value we can return that is not definable, 
and throwing an exception isn't an option.

You have 2 ways to check it further if you do get false back.  
defined('class::constant') will tell you if it is defined and ReflectionClass 
::getConstants() will give you an array of all the defined constants along with 
their values.  It is trivial to wrap this and throw your own exception if the 
constant isn't defined.
 [2010-10-29 18:25 UTC] agent_harris at secure-mail dot biz
hmm, ok the situation regarding NULL values i have obviously overseen.

the question is imho not how trivial it is to wrap it (sure using getConstants is an option). but why is throwing an exception not an option. it is what any other good language or library would do. but if it has something to do with compatibility for legacy code then maybe it should be considered in previous php versions to introduce something like a "strict option" for newer code. otherwise new functionality will always get wrapped.
 [2010-10-29 18:49 UTC] rasmus@php.net
We can't change things to suddenly start throwing exceptions because uncaught 
exceptions are fatal and it would break all sorts of code.  Plus nothing in core 
PHP throws exceptions by default.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 13:01:30 2024 UTC