PHP 7 typed and difference between argument numbers and parameters

Asked

Viewed 185 times

0

Reading in:

https://secure.php.net/manual/en/mi

found out the following example:

function arraysSum(array ...$arrays): array
{
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
}

print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

But understand that arraysSum in its definition has only 1 argument;

arraysSum(array ...$arrays)

But on the call are passed 3 arguments

arraysSum([1,2,3], [4,5,6], [7,8,9])

My question is this::

Let’s say I need to define a second argument for this function as below.

function arraysSum(array ...$arrays, int numero): array
{
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
}

$numero = 125;
print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]), $numero);

This will generate an error.

What to do to use typed language in these cases knowing that all parameters passed in the function will be summed?

There seems to be a contradiction between the number of function parameters and the number of arguments passed in the call.

I don’t quite understand it!

My goal is to do something like:

<?php

function teste( string... $_array, string a ) : array {

    return array_push($_array, a);

}

print_r( teste (...["teste","2"], "adicionado") );

but without the 3 points in the ...["teste", "2"].

Is there any way?

That’s the mistake you’re making:

Parse error: syntax error, unexpected 'a' (T_STRING), expecting variable (T_VARIABLE) in /var/www/html/gasmuriae.com.br/web/gceu/teste.php on line 10
  • I didn’t understand the point of the question. When you set a parameter with ... means that the number of arguments passed is variable can be 1,2,3, 10 etc.

  • it is. So it seems that it was me who did not understand very well. The goal is for these points to reference a ttipado array of strings

  • In php there is no Generics hack (that language created by facebook on top of php) has.

  • Okay, but what would be the case if I had to pass an array of strings to the function as one of the arguments because the second parameter of the function requires a string for example? This is my difficulty.

1 answer

1

What you need is not Operator spread, the ..., then there is no way to use it to do what you wish.

The Operator spread groups all parameters passed to function within one array. If you use it in the first parameter, it means that all will belong to the array, so setting a second parameter will give error. You may have other parameters before, but not after. Read more in the official documentation Variable number of arguments.

What you need is only two parameters, one of which is array:

function my_push(array $arr, string $value): array {
    array_push($arr, $value);
    return $arr;
}

print_r(my_push(["teste","2"], "adicionado"));

Staying:

Array
(
    [0] => teste
    [1] => 2
    [2] => adicionado
)

Solution "Kids don’t do that at home"

You have a saw and a hammer, and you need to fix a nail. This solution shows you how to use the saw _(ツ)_/

By reversing the positions of the parameters you can get something similar to what you want, but it is wrong to use the operator spread.

function my_push(string $value, string ...$arr): array {
    array_push($arr, $value);
    return $arr;
}

print_r(my_push('adicionado', ...[1, 2]));

Resulting:

Array
(
    [0] => 1
    [1] => 2
    [2] => adicionado
)

But using the operator spread, will also allow to make my_push(1, 2, 3), producing the same result. Read only this function call and tell me who is being added to whom? Bad for maintenance, don’t do that. It will only be a matter of time to repent - if you work as a team, that time will decrease significantly.

Realize that even putting the type string in the parameter PHP accepted I pass a list of integers. It’s PHP, it does it. It lets you pass a list of integers, but will do the casting for string. You pass a list of integers and receive a list of strings - another thing that only complicates the life of those who are reading the code.

  • yes, perfect. But even I had already succeeded in other tests. What I need to say is that this array will be typed and will ONLY be able to receive STRING types. In another post,(https://answall.com/questions/354586/array-espec%C3%Adfico-de-strings-in-php-%C3%A9-poss%C3%Advel) I did string ... $parameter resolver. But when I entered a second parameter it didn’t work. You can help me in this situation?

  • added data to the question

  • 1

    You can even do something like that, but it’s using the operator the wrong way, so it’s not worth it. If you need to ensure that values are strings, loop and validate all.

  • yes, but how would it look in the type you spoke: (but it’s using the operator in the wrong way)

  • hum, got it. But...what if I add declare(strict_types=1); at the beginning of the file. This guarantees me the obligation of typing. No?

  • Yes, but I would still allow my_push('1', '2', '3'), that it is not clear what it does.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.