-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathsleep_and_wakeup.phpt
259 lines (190 loc) · 5.86 KB
/
sleep_and_wakeup.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
--TEST--
PHP Spec test generated from ./classes/sleep_and_wakeup.php
--FILE--
<?php
/*
+-------------------------------------------------------------+
| Copyright (c) 2014 Facebook, Inc. (http://www.facebook.com) |
+-------------------------------------------------------------+
*/
error_reporting(-1);
class Point
{
private static $pointCount = 0;
private static $nextId = 1;
private $x;
private $y;
private $id;
// protected $proti;
// public $pubi = FALSE;
// const CON = 10; // excluded from serialization
// protected static $prots; // excluded from serialization
public static function getPointCount()
{
return self::$pointCount;
}
public function __construct($x = 0, $y = 0)
{
$this->x = $x;
$this->y = $y;
++self::$pointCount;
$this->id = self::$nextId++;
echo "\nInside " . __METHOD__ . ", $this, point count = " . self::$pointCount . "\n\n";
}
public function move($x, $y)
{
$this->x = $x;
$this->y = $y;
}
public function translate($x, $y)
{
$this->x += $x;
$this->y += $y;
}
public function __destruct()
{
--self::$pointCount;
echo "\nInside " . __METHOD__ . ", $this, point count = " . self::$pointCount . "\n\n";
}
public function __toString()
{
return 'ID:' . $this->id . '(' . $this->x . ',' . $this->y . ')';
}
///*
public function __sleep()
{
echo "\nInside " . __METHOD__ . ", $this, point count = " . self::$pointCount . "\n\n";
// return array('y', 'x', 'proti', 'pubi');
return array('y', 'x'); // get serialized in array insertion order
// return array('y');
// return array();
// return;
// return NULL by having no return statement
}
//*/
///*
public function __wakeup()
{
echo "\nInside " . __METHOD__ . ", $this, \$nextId, = " . self::$nextId . "\n\n";
++self::$pointCount;
$this->id = self::$nextId++;
}
//*/
}
echo "---------------- create and destroy a Point to boost id -------------------\n";
$a = new Point(1, 1);
unset($a);
echo "---------------- create, serialize, and unserialize a Point -------------------\n";
$p = new Point(-1, 0);
echo "Point \$p = $p\n";
$s = serialize($p); // all instance properties get serialized
var_dump($s);
echo "------\n";
$v = unserialize($s); // without a __wakeup method, any instance property present
// in the string takes on its default value.
var_dump($v);
echo "---------------- Serialize and unserialize NULL -------------------\n";
$s = serialize(NULL); // simulate __sleep not having a return statement or returning nothing
var_dump($s);
$v = unserialize($s);
var_dump($v);
///*
echo "---------------- Add a dynamic property and serialize -------------------\n";
$p->newProp = "abc";
$s = serialize($p); // dynamic property gets serialized if there is NO __sleep method;
// otherwise, __sleep has to take care of that.
var_dump($s);
//*/
///*
class ColoredPoint extends Point
{
const RED = 1;
const BLUE = 2;
private $color;
public function __construct($x = 0, $y = 0, $color = RED)
{
parent::__construct($x, $y);
$this->color = $color;
echo "\nInside " . __METHOD__ . ", $this\n\n";
}
public function __toString()
{
return parent::__toString() . $this->color;
}
// while this method returns an array containing the names of the two inherited, private
// properties and adds to that the one private property from the current class,
// serialize runs in the context o fthe type of the object given it. If that type is
// ColoredPoint, serialize doesn;t knopw what to do when it comes across the names of the
// inherited, private properties.
/*
public function __sleep()
{
echo "\nInside " . __METHOD__ . ", $this\n\n";
$a = parent::__sleep();
var_dump($a);
$a[] = 'color';
var_dump($a);
return $a;
}
*/
}
//*/
///*
echo "---------------- Serialize ColoredPoint -------------------\n";
$cp = new ColoredPoint(9, 8, ColoredPoint::BLUE);
echo "ColoredPoint \$cp = $cp\n";
$s = serialize($cp);
var_dump($s);
$v = unserialize($s);
var_dump($v);
//*/
echo "---------------- end -------------------\n";
--EXPECTF--
---------------- create and destroy a Point to boost id -------------------
Inside Point::__construct, ID:1(1,1), point count = 1
Inside Point::__destruct, ID:1(1,1), point count = 0
---------------- create, serialize, and unserialize a Point -------------------
Inside Point::__construct, ID:2(-1,0), point count = 1
Point $p = ID:2(-1,0)
Inside Point::__sleep, ID:2(-1,0), point count = 1
string(55) "O:5:"Point":2:{s:8:"