Fusion / s2ajax

The awesome Sajax for PHP, but for PHP5, with objects support.

This URL has Read+Write access

s2ajax / example_persistence.php
100644 72 lines (61 sloc) 1.513 kb
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
<?
require("S2ajax.php");
$s2ajax_request_type = "GET";
// Note that exported methods are static!
s2ajax_export_class(
InstanceTester,
array(
'set_value',
'get_value'
)
);
 
s2ajax_export_class(CounterTester);
 
// And now, let's handle the user's request
s2ajax_handle_client_request();
 
class InstanceTester
{
// Our little buddy will be happily serialized
private $value;
// See the variable below? It is static therefore it cannot be persisted: poof!
private static $staticvalue;
 
public function set_value($value) {
$this->value = $value;
return "Assigned $value to class";
}
 
public function get_value() {
return "Retrieve value = ".$this->value;
}
}
 
class CounterTester
{
private $counter;
 
function __construct() {
$this->counter = 0;
}
 
public function increment_counter() {
$this->counter++;
return $this->counter;
}
}
 
?>
<html>
<head>
<script>
<?
s2ajax_show_javascript();
?>
function display_result(val) {
alert(val);
}
 
var var_a = new InstanceTester();
var var_b = new InstanceTester();
var counter = new CounterTester();
</script>
<body>
<button onclick="var_a.set_value('[A]', display_result);">Set instance 'a'</button>
<button onclick="var_b.set_value('[B]', display_result);">Set instance 'b'</button>
<button onclick="var_a.get_value(display_result);">Get instance 'a'</button>
<button onclick="var_b.get_value(display_result);">Get instance 'b'</button>
<button onclick="counter.increment_counter(display_result);">Increment counter</button>
</body>
</html>