forked from krakjoe/pthreads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolatile-objects.phpt
70 lines (59 loc) · 1.73 KB
/
volatile-objects.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
--TEST--
Test Volatile objects
--DESCRIPTION--
Threaded members of Threaded objects are immutable, for the sake of performance and ease of use.
Volatile objects' members are excempt from immutability, but can still be an immutable property.
This test both demonstrates and tests volatility and immutability.
--FILE--
<?php
class Member extends Volatile {
public function method(array $thing) {
return $this->thing = $thing; # fine, coerced to volatile ...
}
public function setThreaded() {
$this->threaded = new Threaded(); # fine, volatile, threaded without refs, still works
$this->threaded->merge($this->thing); # fine, but expensive
}
public function condition() {
return true;
}
}
class Test extends Thread {
public function __construct(Member $member) {
$this->member = $member; # immutable, Volatile is still Threaded and this is a normal
# Threaded object (Thread extends Threaded)
}
public function run() {
$members = [];
if ($this->member->condition()) { # calls to Volatile object methods still fast for this object
$this->member
->method([1,2,3,4,5]);
var_dump($this->member->thing); # fine will display ^Threaded
$this->member->setThreaded(); # fine not immutable because volatile
$this->member = new Threaded(); # not fine, immutable
}
}
}
$member = new Member();
$test = new Test($member);
$test->start();
$test->join();
?>
--EXPECTF--
object(Volatile)#%d (%d) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
}
Fatal error: Uncaught RuntimeException: Threaded members previously set to Threaded objects are immutable, cannot overwrite member in %s:33
Stack trace:
#0 [internal function]: Test->run()
#1 {main}
thrown in %s on line 33