php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70941 Optional parameter identified as required
Submitted: 2015-11-19 13:36 UTC Modified: 2015-11-19 18:21 UTC
From: devosc at gmail dot com Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 7.0.0RC7 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: devosc at gmail dot com
New email:
PHP Version: OS:

 

 [2015-11-19 13:36 UTC] 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 ]
      }
    }
  }
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-19 18:21 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-11-19 18:21 UTC] requinix@php.net
> 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.
 [2015-11-19 22:07 UTC] devosc at gmail dot com
Okay thanks. It seems like I should be using isDefaultValueAvailable() instead.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC