Hi,

 ---- En vie, 10 jul 2020 16:18:40 +0200 Benjamin Eberlei <kont...@beberlei.de> 
escribió ----
 > On Fri, Jul 10, 2020 at 11:21 AM Marco Pivetta <ocram...@gmail.com> wrote:
 > 
 > > Hi Nikita,
 > >
 > > I kept my "NO" stance here, as per discussion in
 > > https://externals.io/message/110004#110005, where I provided (in my
 > > opinion) good/safe alternatives to arrays as input parameters.
 > >
 > 
 > > The BC implications on this RFC still largely outweigh any advantages that
 > > it brings, from my perspective.
 > >
 > 
 > Are there alternatives to named parameters? Of course. Are they as simple?
 > Not really.
 > 
 > 1. You can use an array, but array values cannot be typed, so you completly
 > loose typing of the language.
 > 2. You can use an object with builder pattern, but that requires a lot of
 > code to write, and increases the mental capacity of understanding something
 > for developers.
 > 3. your example of an api to convert named params to a sequence is not
 > discoverable, you don't know looking or using method A() that it comes with
 > an additional NamedParamsA() helper.
 > 


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
```

( name of function can be better )

in order to check types in arrays. Then you could:

/**
  * @param array $args{name: string, surname: string, ?age: int}
 */
function my_function(  array $args ) {
   $scheme = ['name' => 'string', 'surname' => 'string', '?age' => 'int');
   $is_valid =  array_check_scheme( $args, $scheme );
}


my_function( ['name' = 'Nikita', 'surname' => 'Popov' ] );  //is_vaild = true
my_function( ['name' = 'Nikita'] );  //is_vaild = false
my_function( ['name' = 'Nikita'],  'age' => 10 );  //is_vaild = false

A function like this could be useful in other contexts as well.

Regards
--
Manuel Canga

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to