Replacing the … wildchar in configuration files
- David Grudl
- Nette Core | 8218
The ...
is used in the configuration file to skip a parameter
that DI then autowires:
class Facade
{
function __construct(Database $db, int $id)
{
}
}
services:
- Facade(..., 123)
# alternative way:
- Facade(id: 123)
This wildchar was created ten years ago (yes, DI is really that old). At that time there were no variadics parameters to be confused with. PHP 8.1 comes with a first-class callable syntax that looks exactly the same but means something completely different.
Now the three dots symbol must be changed to something else.
I suggest using either --
or *
or
@
:
services:
- Facade(--, 123)
- Facade(*, 123)
- Facade(@, 123)
Single -
has special meaning in Neon, cannot be used. The
*
is common wildchar. And the @
already has meaning as
the service reference.
Update: added _
:
services:
- Facade(_, 123)
- Rick Strafy
- Nette Blogger | 81
I didn't even know about that syntax, is there any real use-case where is
Facade(..., 123)
preferred over Facade(id: 123)
? Btw,
*
seems more intuitive.
//EDIT: I thought *
will escape all previous parameters, if the
usage is that it will ignore only that one parameter, then _
is the
way to go Facade(_, _, 123)
, although I think it should be
deprecated, since id: 123
is more readable, and relying on the
parameter order is just stupid idea.
Last edited by Rick Strafy (2021-09-21 16:39)
- Jan Tvrdík
- Nette guru | 2595
Both Scala and Kotlin use underscore.
services:
- Facade(_, _, 123)
But in most cases Facade(id: 123)
should be preferred.
- David Grudl
- Nette Core | 8218
Named arguments mean you rely on name constancy, dots mean you rely on order constancy. Both have a sense imho.
- vojtamares
- Member | 26
Disclaimer: I write some PHP for Czech Scouts in my freetime, my day job is Go and YAML.
From the perspective of PHP, it has support for named arguments since 8.0, IMHO it is the “native” way" to solve this.
In Go (and other languages like Rust if I remember correctly), underscore
_
is used for dropping a value while declaring multiple variables
(e.g. function returns a value and an error, we care only about the error atm.)
or dummy imports.
Therefore underscore is IMHO acceptable for this case.
*
is no go, since it's a common wildcard as mentioned before@
refers services in neon and has a functionality in PHP itself → no--
it is just weird, for me same weirdness as proposed attributes with@@
prefix (pls don't)
TL;DR
- named attributes FTW
_
is ok
- srigi
- Nette Blogger | 558
Maybe inspiration from EcmaScript 6 world could be valuable here to:
services:
- Facade(, 123)
- Slava.Aurim
- Member | 19
I vote for the consistency of the neon-config syntax with other PHP constructs. Compare:
[,,$banana,$pear] = fruits();
services:
- Facade(,, 123)
- David Grudl
- Nette Core | 8218
So I used the character _
that got the most responses https://github.com/…a719326ab6d8