1--TEST--
2SPL: ArrayObject::exchangeArray() basic usage with object as underlying data store.
3--FILE--
4<?php
5
6class C {
7    public $pub1 = 'public1';
8}
9
10echo "--> exchangeArray() with objects:\n";
11$original = new C;
12$ao = new ArrayObject($original);
13$swapIn = new C;
14try {
15    $copy = $ao->exchangeArray($swapIn);
16    $copy['addedToCopy'] = 'added To Copy';
17} catch (Exception $e) {
18    echo "Exception:" . $e->getMessage() . "\n";
19}
20$swapIn->addedToSwapIn = 'added To Swap-In';
21$original->addedToOriginal = 'added To Original';
22var_dump($ao, $original, $swapIn, $copy);
23
24
25echo "\n\n--> exchangeArray() with no arg:\n";
26unset($original, $ao, $swapIn, $copy);
27$original = new C;
28$ao = new ArrayObject($original);
29try {
30    $copy = $ao->exchangeArray();
31    $copy['addedToCopy'] = 'added To Copy';
32} catch (TypeError $e) {
33    echo "Exception: " . $e->getMessage() . "\n";
34}
35$original->addedToOriginal = 'added To Original';
36var_dump($ao, $original, $copy);
37
38echo "\n\n--> exchangeArray() with bad arg type:\n";
39unset($original, $ao, $swapIn, $copy);
40$original = new C;
41$ao = new ArrayObject($original);
42try {
43    $copy = $ao->exchangeArray(null);
44    $copy['addedToCopy'] = 'added To Copy';
45} catch (TypeError $e) {
46    echo $e->getMessage() . "\n";
47}
48$original->addedToOriginal = 'added To Original';
49var_dump($ao, $original, $copy);
50
51?>
52--EXPECTF--
53--> exchangeArray() with objects:
54object(ArrayObject)#2 (1) {
55  ["storage":"ArrayObject":private]=>
56  object(C)#3 (2) {
57    ["pub1"]=>
58    string(7) "public1"
59    ["addedToSwapIn"]=>
60    string(16) "added To Swap-In"
61  }
62}
63object(C)#1 (2) {
64  ["pub1"]=>
65  string(7) "public1"
66  ["addedToOriginal"]=>
67  string(17) "added To Original"
68}
69object(C)#3 (2) {
70  ["pub1"]=>
71  string(7) "public1"
72  ["addedToSwapIn"]=>
73  string(16) "added To Swap-In"
74}
75array(2) {
76  ["pub1"]=>
77  string(7) "public1"
78  ["addedToCopy"]=>
79  string(13) "added To Copy"
80}
81
82
83--> exchangeArray() with no arg:
84Exception: ArrayObject::exchangeArray() expects exactly 1 argument, 0 given
85
86Warning: Undefined variable $copy in %s on line %d
87object(ArrayObject)#2 (1) {
88  ["storage":"ArrayObject":private]=>
89  object(C)#3 (2) {
90    ["pub1"]=>
91    string(7) "public1"
92    ["addedToOriginal"]=>
93    string(17) "added To Original"
94  }
95}
96object(C)#3 (2) {
97  ["pub1"]=>
98  string(7) "public1"
99  ["addedToOriginal"]=>
100  string(17) "added To Original"
101}
102NULL
103
104
105--> exchangeArray() with bad arg type:
106ArrayObject::exchangeArray(): Argument #1 ($array) must be of type array, null given
107
108Warning: Undefined variable $copy in %s on line %d
109object(ArrayObject)#3 (1) {
110  ["storage":"ArrayObject":private]=>
111  object(C)#2 (2) {
112    ["pub1"]=>
113    string(7) "public1"
114    ["addedToOriginal"]=>
115    string(17) "added To Original"
116  }
117}
118object(C)#2 (2) {
119  ["pub1"]=>
120  string(7) "public1"
121  ["addedToOriginal"]=>
122  string(17) "added To Original"
123}
124NULL
125