The Storyteller

An interesting bug in ReflectionParameter object in PHP 5.2.1

May 13, 2007 · 12 Comments

I came across an interesting bug last night. I searched for the reference and found that Sebastian Bergman reported quite similar bug in 11 Jun 2005 (Bug # 33312) which is currently marked as closed. I tested that bug reported by Sebastian and well, its fixed.

But there is still the following bug alive in ReflectionParameter object, I tested it against the PHP version 5.2.1 . So what is this bug? The reflection parameter cannot retrieve the default value of a parameter if the next parameter has no default value. PHP simply omits all the variables before that variable and return only values after that variable (I am sorry for my poor english). To regenerate the bug, you can execute the following script

<?
class TestClass
{
public function __construct($a , $b=6 , $c = “hh”, $d, $e=76)
{

}
}

$class = new ReflectionClass(”TestClass”);
$method= $class->getMethod(”__construct”);
$parameter = $method->getParameters();

foreach ($parameter as $param)
{
if ($param->isDefaultValueAvailable())
{
if (is_null($param->getDefaultValue()))
echo $param->getName().” : null \n”;
else
echo $param->getName().” : “.$param->getDefaultValue().”\n”;
}
}
?>

The above code returns only “e : 76″ which is definitely not the expected value. And if you modify the TestClass like below

class TestClass
{
public function __construct($a = “1″, $b , $c = “string”, $e=76)
{

}
}

it will output
c : string
e : 76

Its a bug, right? I donno if already reported! And I didn’t test it in 5.2.2 but i guess it’s still there.

Categories: PHP · bug

12 responses so far ↓

  • Martin // May 13, 2007 at 3:00 pm

    Result with 5.2.2:

    ——-
    e : 76
    ——-

  • hasin // May 13, 2007 at 3:12 pm

    Thanks Martin, That indicates the bug is also alive in 5.2.2

  • Johannes Schlüter // May 13, 2007 at 3:15 pm

    Hi,

    you already searched through the bug database., Why didn’t you continue with it and reported the issue as an bug? Reporting a bug increases the chances that we find these reports and fix them. You have a bit luck that Sebastian read your post and sent me the link, but still I ask you to report a bug so we can keep track of this problem and fix it.

    Blogs are nice for sharing opinions but they won’t work to manage bug reports and feature requests.

    johannes

  • hasin // May 13, 2007 at 3:41 pm

    Thanks Johannes

    the bug is reported here http://bugs.php.net/bug.php?id=41382

  • sopel // May 13, 2007 at 4:23 pm

    However, I think putting non-optional params after optional params (i.e. with default values) is not good programming practice at all.

  • Johannes Schlüter // May 13, 2007 at 4:28 pm

    Hi again,

    ok, after my first coffee and breakfast I took a look on your problem and saw that your assumption was wrong.

    In your first example $b and $c aren’t optional since an parameter without default value ($d) is present, too. We don’t store these values since the engine needs default values only for optional parameters.

    See also example 17.9 ant the note above it on http://de3.php.net/manual/en/functions.arguments.phpbe that the parser doesn’t.

    Sorry for not seeing it the first time :-)

  • hasin // May 13, 2007 at 4:43 pm

    Thanks Johannes

  • Matthew Weier O'Phinney // May 13, 2007 at 6:08 pm

    Your code is invalid PHP. As the manual (http://php.net/functions) says, “when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.” The problem you’re running into is that you’re mixing arguments without defaults with arguments having defaults. $d should occur before $c.

  • developercast.com » Hasin Hayder’s Blog: An interesting bug in ReflectionParameter object in PHP 5.2.1 // May 14, 2007 at 9:25 pm

    [...] a new blog post today, Hasin Hayder points out an interesting bug he found in the Reflection functionality [...]

  • nhm tanveer hossain khan (hasan) // May 15, 2007 at 12:38 am

    it seems a true bug on PHP engine. if you ask where is the bug. i must say bug of not reporting the wrong coding.

    PHP interpreter should ended with throwing a runtime | interpretation time | compile time exception.

    developers are human and human are used to make wrong. if it is written over the documentation, it is absolutely the work of lazy developer who wrote that part of code on php engine.

    hope this problem will be resolved as soon as possible. and this kinda stuff won’t be excused as documented stuff..

    best regards,

  • Samiha Esha // May 15, 2007 at 11:44 am

    Nice observation

  • Rajputro // May 17, 2007 at 9:55 am

    This is an off topic comment, I guess I should mark it as [OT]
    Anyway, I’ve been interviewed at escenic for j2ee developer, and from 25th of last month to 15th May I’ve visited somewherein office 4 times :D. But unfortunately my timing was so poor, that none of the occasions I had the chance to meet you, as you were not there at those times. :(
    Anyway stay well and pray for me. :)

    Shafi

Leave a Comment