Bug #70941 [Opn->Nab]: Optional parameter identified as required
| From: | requinix@php.net | Date: | Thu, 19 Nov 2015 18:21:10 +0000 |
| Subject: | Bug #70941 [Opn->Nab]: Optional parameter identified as required | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-197337@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70941&edit=1
ID: 70941
Updated by: requinix@php.net
Reported by: devosc at gmail dot com
Summary: Optional parameter identified as required
-Status: Open
+Status: Not a bug
Type: Bug
Package: Reflection related
PHP Version: 7.0.0RC7
Block user comment: N
Private report: N
New Comment:
> The order in which an optional parameter is specified should not matter.
Of course it matters. To construct B you have to specify the required $a, but since that's the
second argument you have to specify the first argument $b too.
Sure, you defined it as optional, but there's no way to call the function/method without
passing it. So in truth it's actually required.
And there's the ominous warning the docs give, too:
> Note that when using default arguments, any defaults should be on the right side
> of any non-default arguments; otherwise, things will not work as expected.
Previous Comments:
------------------------------------------------------------------------
[2015-11-19 13:36:27] devosc at gmail dot com
Description:
------------
The order in which an optional parameter is specified should not matter. However, currently, when an
optional parameter occurs before a required parameter in a constructor or function argument, the
Reflection package is incorrectly identifying the optional parameter as being a required parameter.
Test script:
---------------
<?php
class A {}
class B {
function __construct($b = null, A $a) {}
}
Reflection::export(new ReflectionClass('B'));
class C {
function __construct(A $a, $b = null) {}
}
Reflection::export(new ReflectionClass('C'));
https://3v4l.org/gKhZ6
https://3v4l.org/Yt2JN
Expected result:
----------------
Parameter b should be optional regardless of its position in a constructor or function argument.
Actual result:
--------------
Class [ <user> class B ] {
@@ ./index.php 10-12
- Constants [0] {
}
- Static properties [0] {
}
- Static methods [0] {
}
- Properties [0] {
}
- Methods [1] {
Method [ <user, ctor> public method __construct ] {
@@ ./index.php 11 - 11
- Parameters [2] {
Parameter #0 [ <required> $b ]
Parameter #1 [ <required> A $a ]
}
}
}
}
Class [ <user> class C ] {
@@ ./index.php 16-18
- Constants [0] {
}
- Static properties [0] {
}
- Static methods [0] {
}
- Properties [0] {
}
- Methods [1] {
Method [ <user, ctor> public method __construct ] {
@@ ./index.php 17 - 17
- Parameters [2] {
Parameter #0 [ <required> A $a ]
Parameter #1 [ <optional> $b = NULL ]
}
}
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70941&edit=1