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

Dedicated syntax for variadic parameters #421

Closed
wants to merge 1 commit into from
Closed

Conversation

nikic
Copy link
Member

@nikic nikic commented Aug 27, 2013

Implementation for https://wiki.php.net/rfc/variadics RFC.

@clemherreman
Copy link

RFC can be found here: https://wiki.php.net/rfc/variadics

I genuinely find this a good thing, especially for the type hinting. Being able to explicitely indicate that a function is variadic via its signature is way better than looking at documentation or directly by looking at its code.

So a big +1 :)

@failpunk
Copy link

Is the syntax space sensitive? Same examples have a space after the ellipsis and some do not.

public function query($query, ...$params) 

public function prepare($query, &... $params)

@nikic
Copy link
Member Author

nikic commented Aug 28, 2013

@failpunk No, whitespace doesn't matter (as with all of PHP).

@@ -11,4 +11,4 @@ class test {
echo "Done\n";
?>
--EXPECTF--
Fatal error: Method test::__clone() cannot accept any arguments in %s on line %d
Fatal error: Method test::__clone() cannot take arguments in %s on line %d
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change I wonder?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored some of the magic method implementation checks (and changed the error message doing that), but that turned out to be unnecessary in the end. I now removed the changes to avoid cluttering the diff.

@olekukonko
Copy link

Its good to see PHP is about to support variadic functions just like in go but slightly different. I would like want to know if this code below return expected output :

function  A($x,$y,$z) {

    printf("%d,%d,%d",$x,$y,$z);
}

function B(...$vars) {
    A(...$vars);
}

function C($no, ...$vars) {
    foreach ($vars as &$var) {
        $var = $var * $no ;
    }
    A(...$vars);
}

B(1,2,3); // Expect  1,2,3  
C(2,1,2,3); // Expect  2,4,8  

Would this work this way or how do you achieve this with current implementation. Secondly can arrays be simple used. eg

A(...[1,10,100]); // Expect 1,10,100

//OR

$array = array_reverse(range(1,100))
A(...$array) // Expect 100,99,98 

I really this not see clarification of this usage case in the RFC. Can you please clarify ?

@smalyshev
Copy link
Contributor

@olekukonko you can always use call_user_func_array() for that. For the last example, I don't even see why would you bother to write A(...[1,10]) if you can just write A(1,10).

@olekukonko
Copy link

@smalyshev that was just an example .... there is so many use cases for A(...$array)

@smalyshev
Copy link
Contributor

@olekukonko All of which, as far as I can see, are covered by call_user_func_array().

@slavcodev
Copy link

May be use function fn(&$var, ...&$params) instead of function fn(&$var, &...$params)
It is more consistent.

@php-pulls
Copy link

Comment on behalf of nikic at php.net:

Has been merged.

@php-pulls php-pulls closed this Sep 30, 2013
@kaplanlior
Copy link
Contributor

For future reference, commit SHA1 is 0d7a638

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants