Ajaxdata.php
1.6 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
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Handles data preparation for XMLHTTP responses to xmlhttpHandler script
*
* @author Chris Boden
* @version 0.3
*/
class Ajaxdata {
private $_data = Array();
private static $exception = false;
/**
* @private
*/
final public function __destruct() {
if (self::$exception) {
return false;
}
$output = Array();
$output['status'] = 0;
$output['data'] = $this->_data;
echo json_encode($output);
}
/**
* @private
*/
final public function __set($var, $val) {
// is this going to work for arrays?
$this->_data[$var] = $val;
}
/**
* @private
*/
final public function &__get($var) {
if (isset($this->_data[$var])) {
return $this->_data[$var];
} else {
return '';
}
}
/**
* @private
* possibly obsolete
*/
final public static function ExceptionThrown() {
self::$exception = true;
}
}
/**
* Only 1 is declared at a time
* This exception is generated when doing an XHR
*
* @author Chris Boden
* @version 0.2
*/
class JSONException extends Exception {
/**
* @private
*/
public function __construct($message=NULL, $code=0) {
parent::__construct($message, $code);
// die instead?
Ajaxdata::ExceptionThrown();
}
/**
* @private
*/
public function __destruct() {
$output = Array();
$output['status'] = 1;
$output['data'] = Array('message' => $this->getMessage());
echo json_encode($output);
}
}
?>