|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61602 Allow access to name of constant used as default value
Submitted: 2012-04-02 14:14 UTC Modified: 2012-05-15 09:17 UTC
From: Assigned: sebastian
Status: Closed Package: Reflection related
PHP Version: 5.4Git-2012-04-02 (Git) OS: Irrelevant
Private report: No CVE-ID:
 [2012-04-02 14:14 UTC]
When a constant is used for the default value of an optional parameter, the Reflection API (tries to) resolve(s) the constant to its value and then returns it as the default value.

In addition, I would like to have two additional methods in ReflectionParameter that expose 1) whether or not a default value "is a" constant and 2) the name of the constant.

In the example below, I am using temporary names ("defaultValueIsConstant" and "getDefaultValueConstantName") for these two methods because I cannot think of sensible method names right now.

Test script:
class Foo {
    const bar = '...';

    public function baz($bar = self::bar) {

$m = new ReflectionMethod('Foo', 'baz');
$p = $m->getParameters();

foreach ($p as $_p) {

    if ($_p->defaultValueIsConstant()) {

Expected result:
string(3) "..."
string(9) "self::bar"

Actual result:
string(3) "..."


getDefaultValueConstantName.diff (last revision 2012-04-03 00:23 UTC) by

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-02 14:34 UTC]
It's definitely possible, I've just added something like this to VLD:
 [2012-04-03 00:23 UTC]
The following patch has been added/updated:

Patch Name: getDefaultValueConstantName.diff
Revision:   1333412604
 [2012-04-03 00:24 UTC]
Just created a quick patch. If it's Ok with everybody I'll commit it to master 
with tests.
 [2012-04-03 05:56 UTC] reeze dot xia at gmail dot com
Hi, pierrick & sebastian:
   I have create another patch for this request. it differ's in:

   - rename defaultValueIsConstant to isDefaultValueConstant to match bool 
return values's is* method naming.
   - handle global constant.
   - refactor code to remove duplicate.
   - add 3 tests for it.

and I've make test everything looks fine.

please review it for me.

 [2012-04-03 06:07 UTC] reeze dot xia at gmail dot com

@pierrick pull request is here

 [2012-04-03 09:22 UTC]
Any chance this pull request will be merged into PHP 5.4.1? Thanks.
 [2012-04-08 00:45 UTC]
Not very likely for 5.4.1, doesn't seem to be a critical bug. For 5.4.2, maybe.
 [2012-04-25 06:10 UTC]
Can we at least merge the patch into master? Once it's there we can think about merging it into PHP 5.4 for PHP 5.4.2.
 [2012-05-15 07:11 UTC]
Updated example script that uses new method names:

➜  ~  cat test.php 
class Foo {
    const bar = '...';

    public function baz($bar = self::bar) {

$m = new ReflectionMethod('Foo', 'baz');
$p = $m->getParameters();

foreach ($p as $_p) {

    if ($_p->isDefaultValueConstant()) {

➜  ~  php test.php 
string(9) "self::bar"
 [2012-05-15 09:17 UTC]
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at

 For Windows:
Thank you for the report, and for helping us make PHP better.

 [2012-05-15 09:17 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: sebastian
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Thu Nov 26 01:01:34 2015 UTC