new_001.phpt 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. --TEST--
  2. Confirm difference between assigning new directly and by reference.
  3. --INI--
  4. error_reporting=E_ALL | E_DEPRECATED
  5. --FILE--
  6. <?php
  7. echo "Compile-time strict error message should precede this.\n";
  8. class Inc
  9. {
  10. private static $counter = 0;
  11. function __construct()
  12. {
  13. $this->id = ++Inc::$counter;
  14. }
  15. }
  16. $f = new Inc();
  17. $k =& $f;
  18. echo "\$f initially points to the first object:\n";
  19. var_dump($f);
  20. echo "Assigning new object directly to \$k affects \$f:\n";
  21. $k = new Inc();
  22. var_dump($f);
  23. echo "Assigning new object by ref to \$k removes it from \$f's reference set, so \$f is unchanged:\n";
  24. $k =& new Inc();
  25. var_dump($f);
  26. ?>
  27. --EXPECTF--
  28. Deprecated: Assigning the return value of new by reference is deprecated in %s on line 23
  29. Compile-time strict error message should precede this.
  30. $f initially points to the first object:
  31. object(Inc)#%d (1) {
  32. ["id"]=>
  33. int(1)
  34. }
  35. Assigning new object directly to $k affects $f:
  36. object(Inc)#%d (1) {
  37. ["id"]=>
  38. int(2)
  39. }
  40. Assigning new object by ref to $k removes it from $f's reference set, so $f is unchanged:
  41. object(Inc)#%d (1) {
  42. ["id"]=>
  43. int(2)
  44. }