passByReference_006.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. --TEST--
  2. Pass uninitialized objects and arrays by reference to test implicit initialisation.
  3. --FILE--
  4. <?php
  5. function refs(&$ref1, &$ref2) {
  6. $ref1 = "Ref1 changed";
  7. $ref2 = "Ref2 changed";
  8. }
  9. class C {
  10. function __construct(&$ref1, &$ref2) {
  11. $ref1 = "Ref1 changed";
  12. $ref2 = "Ref2 changed";
  13. }
  14. function refs(&$ref1, &$ref2) {
  15. $ref1 = "Ref1 changed";
  16. $ref2 = "Ref2 changed";
  17. }
  18. static function static_refs(&$ref1, &$ref2) {
  19. $ref1 = "Ref1 changed";
  20. $ref2 = "Ref2 changed";
  21. }
  22. }
  23. echo "\n ---- Pass uninitialized array & object by ref: function call ---\n";
  24. unset($u1, $u2);
  25. refs($u1[0], $u2[0][1]);
  26. var_dump($u1, $u2);
  27. echo "\n ---- Pass uninitialized arrays & objects by ref: static method call ---\n";
  28. unset($u1, $u2);
  29. C::static_refs($u1[0], $u2[0][1]);
  30. var_dump($u1, $u2);
  31. echo "\n\n---- Pass uninitialized arrays & objects by ref: constructor ---\n";
  32. unset($u1, $u2);
  33. $c = new C($u1[0], $u2[0][1]);
  34. var_dump($u1, $u2);
  35. echo "\n ---- Pass uninitialized arrays & objects by ref: instance method call ---\n";
  36. unset($u1, $u2);
  37. $c->refs($u1[0], $u2[0][1]);
  38. var_dump($u1, $u2);
  39. ?>
  40. --EXPECT--
  41. ---- Pass uninitialized array & object by ref: function call ---
  42. array(1) {
  43. [0]=>
  44. string(12) "Ref1 changed"
  45. }
  46. array(1) {
  47. [0]=>
  48. array(1) {
  49. [1]=>
  50. string(12) "Ref2 changed"
  51. }
  52. }
  53. ---- Pass uninitialized arrays & objects by ref: static method call ---
  54. array(1) {
  55. [0]=>
  56. string(12) "Ref1 changed"
  57. }
  58. array(1) {
  59. [0]=>
  60. array(1) {
  61. [1]=>
  62. string(12) "Ref2 changed"
  63. }
  64. }
  65. ---- Pass uninitialized arrays & objects by ref: constructor ---
  66. array(1) {
  67. [0]=>
  68. string(12) "Ref1 changed"
  69. }
  70. array(1) {
  71. [0]=>
  72. array(1) {
  73. [1]=>
  74. string(12) "Ref2 changed"
  75. }
  76. }
  77. ---- Pass uninitialized arrays & objects by ref: instance method call ---
  78. array(1) {
  79. [0]=>
  80. string(12) "Ref1 changed"
  81. }
  82. array(1) {
  83. [0]=>
  84. array(1) {
  85. [1]=>
  86. string(12) "Ref2 changed"
  87. }
  88. }