• PHP 扑捉未处理异常 PHP


    PHP如何扑捉未处理异常

    class MyException extends Exception
    {
    	function MyException($message, Exception $ex = NULL)
    	{
    		$this->message = $message;
    		$this->innerException = $ex;
    	}
    	public $innerException = NULL;
    }
    
    
    function exception_handler($exception)
    {
    	$s = '<pre>' . var_export($exception, true) . '</pre>';
    	$s = str_replace("  ", "    ", $s);
    	echo "<b style=\"color:red;\">Uncaught exception</b> " , $s, "\n";
    	error_log($s);
    
    	// php.ini log_errors = On error_log = c:/php_errors.log display_errors = On
    }
    
    set_exception_handler('exception_handler');
    
    
    //error handler function
    function customError($errno, $errstr, $errfile, $errline)
    {
    	echo 'customError<br>';
    	throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
    }
    
    //set error handler
    set_error_handler("customError");
    
    function fnError($num, $div)
    {
    	return $num / $div;
    }
    $fn = 'fnError';
    //$i = $fn(123, 0);
    
    try
    {
    	try
    	{
    		$i = 0;
    		//throw new Exception("出错啦。");
    	}
    	catch (Exception $e)
    	{
    		echo "错误扑捉1";
    		throw new MyException("自定义错误1", $e);
    	}
    }
    catch (Exception $e)
    {
    	echo "错误扑捉2";
    	throw new MyException("自定义错误2", $e);
    }
    

    其他小技巧

    清除缓冲区 ob_clean(); 清除之前所有的echo输出。

    暂停3秒 sleep(3);

    获取页面执行时长:

    $running_time = time() - $_SERVER['REQUEST_TIME'];
    echo '页面运行时间:',$running_time,' 秒<br>';
    

    PHP中的常量

    define("D1", "dddd1");
    
    class MyClass1
    {
    	const APP_NAME = "app";
    }
    
    echo MyClass1::APP_NAME;
    echo D1;
    constant("D1");
    constant("MyClass1::APP_NAME");
    

    PHP的Session

    session_start();
    if ( isset($_SESSION['SID']) )
    {
    	echo $_SESSION['SID'];
    }
    
    $_SESSION["SID"] = "SESSION" . time();
    
    //session_destroy();
    //session_commit();
    

    设置COOKIE setcookie("tt", "cookie");

    输出变量内容

    $s = var_export($_REQUEST, TRUE);
    echo "<pre>$s</pre>";
    

      

  • 相关阅读:
    baomi
    保密|原创解决您的后顾之忧
    为什么选择我们
    c++实现平面上的形状编辑
    完美售后提供完善修改服务
    冰山理论
    边集数组
    图的存储结构(十字链表、邻接多重表、边集数组) 数据结构和算法58
    邻接多重表
    邻接多重表
  • 原文地址:https://www.cnblogs.com/zjfree/p/2934297.html
Copyright © 2020-2023  润新知