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