Allow access to name of constant used as default value
| 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: | sebastian@php.net | Assigned: | sebastian (profile) | ||
| Status: | Closed | Package: | Reflection related | ||
| PHP Version: | 5.4Git-2012-04-02 (Git) | OS: | Irrelevant | ||
| Private report: | No | CVE-ID: | None | ||
[2012-04-02 14:14 UTC] sebastian@php.net
Description:
------------
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:
---------------
<?php
class Foo {
const bar = '...';
public function baz($bar = self::bar) {
}
}
$m = new ReflectionMethod('Foo', 'baz');
$p = $m->getParameters();
foreach ($p as $_p) {
var_dump($_p->getDefaultValue());
if ($_p->defaultValueIsConstant()) {
var_dump($_p->getDefaultValueConstantName());
}
}
Expected result:
----------------
string(3) "..."
string(9) "self::bar"
Actual result:
--------------
string(3) "..."
Patches
getDefaultValueConstantName.diff (last revision 2012-04-03 00:23 UTC by pierrick@php.net)Pull Requests
History
AllCommentsChangesGit/SVN commits
[2012-04-03 00:24 UTC] pierrick@php.net
[2012-04-03 05:56 UTC] reeze dot xia at gmail dot com
[2012-04-03 09:22 UTC] sebastian@php.net
[2012-04-08 00:45 UTC] stas@php.net
[2012-04-25 06:10 UTC] sebastian@php.net
[2012-05-15 07:11 UTC] sebastian@php.net
Updated example script that uses new method names: ➜ ~ cat test.php <?php class Foo { const bar = '...'; public function baz($bar = self::bar) { } } $m = new ReflectionMethod('Foo', 'baz'); $p = $m->getParameters(); foreach ($p as $_p) { var_dump($_p->isDefaultValueConstant()); if ($_p->isDefaultValueConstant()) { var_dump($_p->getDefaultValueConstantName()); } } ➜ ~ php test.php bool(true) string(9) "self::bar"[2012-05-15 09:17 UTC] sebastian@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: sebastian