Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional parameters do not work with PHP 7.1 syntax #37

Closed
odahcam opened this issue Feb 26, 2018 · 7 comments
Closed

Optional parameters do not work with PHP 7.1 syntax #37

odahcam opened this issue Feb 26, 2018 · 7 comments
Labels

Comments

@odahcam
Copy link

odahcam commented Feb 26, 2018

Example:

class SomeController {
    function someAction(?int $id, $request) {}
}

Or:

class SomeController {
    function someAction(?int $id = null, $request) {}
}

These examples does not works, the injector throws an error saying that there's no given value for that parameter. As long as it's optional, something like null should be injected as the value.

@mnapoli
Copy link
Member

mnapoli commented Feb 27, 2018

There are tests covering that: https://github.com/PHP-DI/Slim-Bridge/blob/master/tests/RoutingTest.php#L46-L74 You may be onto an edge case.

Can you try without the ?, and with the optional parameter at the end?

@odahcam
Copy link
Author

odahcam commented Feb 27, 2018

I asked my teamate @anologicon to test it.

@anologicon
Copy link

anologicon commented Feb 27, 2018

👍 Works:

class SomeController {
    function someAction($request, ?int $id = null) {}
}
class SomeController {
    function someAction($request, int $id = null) {}
}

👎 Doesn't Works:

class SomeController {
    function someAction($request, ?int $id) {}
}

Exception :

Exception has occurred.
Invoker\Exception\NotEnoughParametersException: Unable to invoke the callable because no value was given for parameter

@mnapoli mnapoli changed the title Cannot have opitional params Optional parameters do not work with PHP 7.1 syntax Mar 12, 2018
@mnapoli mnapoli added the bug label Sep 8, 2019
@jworreth
Copy link

Hi there,

I'm facing the same issue with optional parameters. Here is a sample of my non-working code:

class ClassController
{
    public $mProxy;
    public function __construct(?Proxy $proxy = null)
    {
        $this->mProxy = $db ?? ProxyFactory::create();
    }
}

And this is the working sample of code:

class ClassController
{
    public $mProxy;
    public function __construct(Proxy $proxy)
    {
        $this->mProxy = $db ?? ProxyFactory::create();
    }
}

As I understand so far, the '?' (question mark) has no meaning to PHP-DI as the behaviour is the same whether I use it or not. My problem appears only when I write Proxy $proxy = null in the constructor of my ClassController.

Would it be possible to have a workaround ? or even more appreciated, a fix ?

Thanks

@mnapoli
Copy link
Member

mnapoli commented Apr 29, 2020

Hi, I haven't had time yet to work on that. However a pull request would be welcome.

@jworreth
Copy link

Thanks a lot for the quick answer, it's much appreciated !

In the meantime though, here is a temporary fix I am using from PHP-DI repo :
Issue 628

I've written a factory used only in my unit test to solve this issue.

Hope this will help someone in my case until a fix will show up :-)

@mnapoli
Copy link
Member

mnapoli commented Oct 12, 2020

Fixed by PHP-DI/Invoker#29

@mnapoli mnapoli closed this as completed Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants