xref: /PHP-8.3/tests/classes/this.phpt (revision f8d79582)
1--TEST--
2ZE2 $this cannot be exchanged
3--FILE--
4<?php
5
6/* please don't shorten this test. It shows what would happen if
7 * the fatal error would have been a warning.
8 */
9class Foo
10{
11    function replace($other)
12    {
13        echo __METHOD__ . "\n";
14        $this = $other;
15        print $this->prop;
16        print $other->prop;
17    }
18
19    function indirect($other)
20    {
21        echo __METHOD__ . "\n";
22        $this = $other;
23        $result = $this = $other;
24        print $result->prop;
25        print $this->prop;
26    }
27
28    function retrieve(&$other)
29    {
30        echo __METHOD__ . "\n";
31        $other = $this;
32    }
33}
34
35$object = new Foo;
36$object->prop = "Hello\n";
37
38$other  = new Foo;
39$other->prop = "World\n";
40
41$object->replace($other);
42$object->indirect($other);
43
44print $object->prop; // still shows 'Hello'
45
46$object->retrieve($other);
47print $other->prop;  // shows 'Hello'
48
49?>
50===DONE===
51--EXPECTF--
52Fatal error: Cannot re-assign $this in %sthis.php on line %d
53