Skip to content

Named params #1

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

Open
wants to merge 3 commits into
base: PHP-5.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions Zend/tests/arg_unpack/string_keys.phpt

This file was deleted.

58 changes: 58 additions & 0 deletions Zend/tests/named_args/basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
Basic named arguments
--FILE--
<?php

function test($a, $b, $c = "c", $d = "d", $e = "e") {
var_dump($a, $b, $c, $d, $e);
}

test(a => "A", b => "B");
test(a => "A", b => "B", c => "C", d => "D", e => "E");
test(d => "D", a => "A", c => "C", e => "E", b => "B");
test(d => "D", b => "B", a => "A");
test(d => "D", b => "B");
test("A", "B", d => "D");
test("A", e => "E", b => "B");

?>
--EXPECTF--
string(1) "A"
string(1) "B"
string(1) "c"
string(1) "d"
string(1) "e"
string(1) "A"
string(1) "B"
string(1) "C"
string(1) "D"
string(1) "E"
string(1) "A"
string(1) "B"
string(1) "C"
string(1) "D"
string(1) "E"
string(1) "A"
string(1) "B"
string(1) "c"
string(1) "D"
string(1) "e"

Warning: Missing argument 1 for test(), called in %s on line %d and defined in %s on line %d

Notice: Undefined variable: a in %s on line %d
NULL
string(1) "B"
string(1) "c"
string(1) "D"
string(1) "e"
string(1) "A"
string(1) "B"
string(1) "c"
string(1) "D"
string(1) "e"
string(1) "A"
string(1) "B"
string(1) "c"
string(1) "d"
string(1) "E"
87 changes: 87 additions & 0 deletions Zend/tests/named_args/by_ref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--TEST--
Named arguments respect by-reference passing semantics
--FILE--
<?php

function test($a, &$b, $c = 0, &$d = 0) {
var_dump($a, $b, $c, $d);
$b++; $d++;
}

test(a => $a1, b => $b1, c => $c1, d => $d1);
var_dump($b1, $d1);

test(c => $c2, b => $b2, d => $d2, a => $a1);
var_dump($b2, $d2);

test(b => $b3, d => $d3);
var_dump($b3, $d3);

$arr1 = [];
test(c => $arr1['c'], b => $arr1['b'], d => $arr1['d'], a => $arr1['a']);
var_dump($arr1);

$test = 'test';
$arr2 = [];
test(c => $arr2['c'], b => $arr2['b'], d => $arr2['d'], a => $arr2['a']);
var_dump($arr2);

?>
--EXPECTF--
Notice: Undefined variable: a1 in %s on line %d

Notice: Undefined variable: c1 in %s on line %d
NULL
NULL
NULL
NULL
int(1)
int(1)

Notice: Undefined variable: c2 in %s on line %d

Notice: Undefined variable: a1 in %s on line %d
NULL
NULL
NULL
NULL
int(1)
int(1)

Warning: Missing argument 1 for test(), called in %s on line %d and defined in %s on line %d

Notice: Undefined variable: a in %s on line %d
NULL
NULL
int(0)
NULL
int(1)
int(1)

Notice: Undefined index: c in %s on line %d

Notice: Undefined index: a in %s on line %d
NULL
NULL
NULL
NULL
array(2) {
["b"]=>
int(1)
["d"]=>
int(1)
}

Notice: Undefined index: c in %s on line %d

Notice: Undefined index: a in %s on line %d
NULL
NULL
NULL
NULL
array(2) {
["b"]=>
int(1)
["d"]=>
int(1)
}
113 changes: 113 additions & 0 deletions Zend/tests/named_args/func_get_args.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
--TEST--
func_get_args() skips arguments that weren't passed
--FILE--
<?php

function test($a, $b = "b", $c = "c", ...$rest) {
var_dump(func_get_args());
var_dump(func_get_arg(0), func_get_arg(1), func_get_arg(2), func_get_arg(3));
var_dump(func_num_args());
}

test(c => "C", a => "A");
test(c => "C", a => "A", d => "D");
test(c => "C", b => "B");
test(c => "C");

function test2($a = FOO, $b = [FOO, BAR], $c = null) {
var_dump(func_get_args());
var_dump(func_get_arg(0), func_get_arg(1), func_get_arg(2));
}

define('FOO', 'FOO value');
define('BAR', 'BAR value');

test2(c => 42);

?>
--EXPECTF--
array(3) {
[0]=>
string(1) "A"
[1]=>
string(1) "b"
[2]=>
string(1) "C"
}

Warning: func_get_arg(): Argument 3 not passed to function in %s on line %d
string(1) "A"
string(1) "b"
string(1) "C"
bool(false)
int(3)
array(3) {
[0]=>
string(1) "A"
[1]=>
string(1) "b"
[2]=>
string(1) "C"
}

Warning: func_get_arg(): Argument 3 not passed to function in %s on line %d
string(1) "A"
string(1) "b"
string(1) "C"
bool(false)
int(3)

Warning: Missing argument 1 for test(), called in %s on line %d and defined in %s on line %d
array(3) {
[0]=>
NULL
[1]=>
string(1) "B"
[2]=>
string(1) "C"
}

Warning: func_get_arg(): Argument 3 not passed to function in %s on line %d
NULL
string(1) "B"
string(1) "C"
bool(false)
int(3)

Warning: Missing argument 1 for test(), called in %s on line %d and defined in %s on line %d
array(3) {
[0]=>
NULL
[1]=>
string(1) "b"
[2]=>
string(1) "C"
}

Warning: func_get_arg(): Argument 3 not passed to function in %s on line %d
NULL
string(1) "b"
string(1) "C"
bool(false)
int(3)
array(3) {
[0]=>
string(9) "FOO value"
[1]=>
array(2) {
[0]=>
string(9) "FOO value"
[1]=>
string(9) "BAR value"
}
[2]=>
int(42)
}
string(9) "FOO value"
array(2) {
[0]=>
string(9) "FOO value"
[1]=>
string(9) "BAR value"
}
int(42)
111 changes: 111 additions & 0 deletions Zend/tests/named_args/internal.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
--TEST--
Named arguments work with internal functions
--FILE--
<?php

// array_fill(int $start_key, int $num, mixed $val)

var_dump(array_fill(start_key => 0, num => 3, val => 42));
var_dump(array_fill(val => 42, start_key => 1, num => 2));
var_dump(array_fill(2, val => 42, num => 1));

// array_slice(array $input, int $offset, int $length = NULL, bool $preserve_keys = false)

var_dump(array_slice(arg => [1, 2, 3, 4, 5], offset => 2, length => 2));
var_dump(array_slice(length => 2, offset => 2, arg => [1, 2, 3, 4, 5]));
var_dump(array_slice(arg => ['a' => 0, 'b' => 1], offset => 1, preserve_keys => true));
var_dump(array_slice(['a' => 0, 'b' => 1], preserve_keys => true, offset => 1));

// missing required arg:
var_dump(array_slice(offset => 2));

// by_ref:
// array_splice(array &$input, int $offset, int $length = 0, mixed $replacement = [])

$array = [1, 2, 3, 4, 5];
var_dump(array_splice(replacement => ["3", "4"], offset => 2, arg => $array));
var_dump($array);

$array = [1, 2, 3, 4, 5];
var_dump(array_splice($array, 2, replacement => ["3", "4"]));
var_dump($array);

?>
--EXPECTF--
array(3) {
[0]=>
int(42)
[1]=>
int(42)
[2]=>
int(42)
}
array(2) {
[1]=>
int(42)
[2]=>
int(42)
}
array(1) {
[2]=>
int(42)
}
array(2) {
[0]=>
int(3)
[1]=>
int(4)
}
array(2) {
[0]=>
int(3)
[1]=>
int(4)
}
array(1) {
["b"]=>
int(1)
}
array(1) {
["b"]=>
int(1)
}

Warning: Parameter 1 missing for array_slice() in %s on line %d
NULL
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
string(1) "3"
[3]=>
string(1) "4"
}
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
string(1) "3"
[3]=>
string(1) "4"
}
Loading