Example #1
0
/**
 * Converts all types to a string
 * 
 * Kindoff mimics var_dump, but only of one variable, and returns the result instead of printing it.
 * 
 * @param mixed $obj	The object that should be converted to a string
 * @return string
 */
function objToString($obj)
{
    if ($obj instanceof Zend_Db_Select) {
        return $obj->__toString();
    } elseif ($obj instanceof Exception) {
        return exceptionToString($obj);
    } elseif (is_bool($obj)) {
        return $obj ? 'bool(true)' : 'bool(false)';
    } elseif (is_string($obj)) {
        return 'string(' . strlen($obj) . ') "' . $obj . '"';
    } elseif (is_null($obj)) {
        return 'NULL';
    } elseif (is_array($obj) || is_object($obj)) {
        return print_r($obj, true);
    } else {
        return $obj;
    }
}
/**
 * Output an exception to the browser and web server error log
 *
 * This method overrides any output already buffered to send and only
 * displays the exception information.
 *
 * @param $ex The exception
 * @param $code Optional. HTTP error code to send. Defaults to 500 Internal Server Error
 */
function displayException($ex, $code = 500)
{
    $msg = exceptionToString($ex);
    error_log("PHP Error:");
    $lines = explode("\n", $msg);
    foreach ($lines as $line) {
        error_log("PHP    " . $line);
    }
    ob_clean();
    // clear anything we were going to send to the browser
    header('HTTP/1.1: ' . $code);
    header('Status: ' . $code);
    print "<pre>" . $msg . "</pre>";
}