Re: [PHP-DEV] [VOTE] Named arguments

This is only part of a thread. view whole thread
  110963
July 12, 2020 12:19 rowan.collins@gmail.com (Rowan Tommins)
On 12 July 2020 11:24:50 BST, Manuel Canga <php@manuelcanga.dev> wrote:
>4. Other option is creating a new function( not yet available in PHP ) >like: > >``` >array_check_scheme( array $array, array $scheme, bool $forced = false >): bool >```
The problem with this, whether built in or not, is that you have to express everything with strings rather than keywords. That means, for instance, that invalid values in the definitions themselves will only error at run-time. The definitions also tend to get verbose pretty quickly. To make it concise and checked at compile-time, you need it to be a construct that pairs identifiers to types without first representing them as strings, e.g. to write `?string $foo = 'hello'` rather than `'foo' => ['type'=>'string', 'nullable'=>true, 'default'=>'hello']`. It turns out we already have a construct for doing that: function signatures. If the input is an actual array or object coming from "outside", you're probably going to want a more extensible validation system anyway. Those are really hard to design, and probably best left to userland where people can choose the tradeoffs they prefer. Regards, -- Rowan Tommins [IMSoP]
  110965
July 12, 2020 14:05 php@manuelcanga.dev (Manuel Canga)
Hi, Rowan,


 ---- En dom, 12 jul 2020 14:19:08 +0200 Rowan Tommins collins@gmail..com> escribió ----

 > The problem with this, whether built in or not, is that you have to express everything with strings rather than keywords. That means, for instance, that invalid values in the definitions themselves will only error at run-time. The definitions also tend to get verbose pretty quickly.
 > 
 > To make it concise and checked at compile-time, you need it to be a construct that pairs identifiers to types without first representing them as strings, e.g. to write `?string $foo = 'hello'` rather than `'foo' => ['type'=>'string', 'nullable'=>true, 'default'=>'hello']`. It turns out we already have a construct for doing that: function signatures.
 > 
 > If the input is an actual array or object coming from "outside", you're probably going to want a more extensible validation system anyway. Those are really hard to design, and probably best left to userland where people can choose the tradeoffs they prefer.
 > 
 > Regards,
 > 

I see. 

However, with named arguments you also have to implement changes in parser in order to can recognize named arguments. You also have to change behavior about default parameters. I don't know if this change affect to performance, maybe do. 

By the other hand, editor like PHPStorm has "Inlay Hints": 
https://www.jetbrains.com/help/rider/Inline_Parameter_Name_Hints.html

With a function which checks scheme, you also could use in other contexts, like  forms:

```
$scheme = ['name' => 'string', 'surname' => 'string', '?age' => 'int');
$is_valid = array_check_scheme( $_POST, $scheme ); 
```

You can use it even with JSON:

```
$request = json_decode( $request_json, true );
$scheme = ['name' => 'string', 'surname' => 'string', '?age' => 'int');
$is_valid = array_check_scheme( $request, $scheme ); 
```

 > e.g. to write `?string $foo = 'hello'` rather than `'foo' => ['type'=>'string', 'nullable'=>true, 'default'=>'hello']`

Maybe you don't need checked at compile-time. is it  slow?, securely, but you don't use in everywhere.

> you're probably going to want a more extensible validation system anyway.
Maybe, in that case, userland libraries have a starting point. With XML parsing is the same. PHP provides basic functionality and userland provide advanced. Regards -- Manuel Canga