Ajaxdata.php 1.6 KB
<?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);
    }
}
?>