__invoke.phpt 979 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. --TEST--
  2. Check that __invoke() works with named parameters
  3. --FILE--
  4. <?php
  5. class Test {
  6. public function __invoke($a = 'a', $b = 'b') {
  7. echo "a: $a, b: $b\n";
  8. }
  9. }
  10. class Test2 {
  11. public function __invoke($a = 'a', $b = 'b', ...$rest) {
  12. echo "a: $a, b: $b\n";
  13. var_dump($rest);
  14. }
  15. }
  16. $test = new Test;
  17. $test(b: 'B', a: 'A');
  18. $test(b: 'B');
  19. try {
  20. $test(b: 'B', c: 'C');
  21. } catch (Error $e) {
  22. echo $e->getMessage(), "\n";
  23. }
  24. echo "\n";
  25. $test2 = new Test2;
  26. $test2(b: 'B', a: 'A', c: 'C');
  27. $test2(b: 'B', c: 'C');
  28. echo "\n";
  29. $test3 = function($a = 'a', $b = 'b') {
  30. echo "a: $a, b: $b\n";
  31. };
  32. $test3(b: 'B', a: 'A');
  33. $test3(b: 'B');
  34. try {
  35. $test3(b: 'B', c: 'C');
  36. } catch (Error $e) {
  37. echo $e->getMessage(), "\n";
  38. }
  39. ?>
  40. --EXPECT--
  41. a: A, b: B
  42. a: a, b: B
  43. Unknown named parameter $c
  44. a: A, b: B
  45. array(1) {
  46. ["c"]=>
  47. string(1) "C"
  48. }
  49. a: a, b: B
  50. array(1) {
  51. ["c"]=>
  52. string(1) "C"
  53. }
  54. a: A, b: B
  55. a: a, b: B
  56. Unknown named parameter $c