1
<?php
2
3
//aop_add_after('Exception->*()',function($jp){var_dump('Exception',$jp->getArguments(),$jp->getMethodName(),$jp->getObject()->getTraceAsString(),'Exception-END');});
4
//aop_add_after('RedisException->*()',function($jp){var_dump('RedisException',$jp->getArguments());});
5
6
//function exception_handler($exception) {var_dump('exception_handler',$exception->getTraceAsString(),'exception_handler_end');}
7
//set_exception_handler('exception_handler');
8
// make set_error_handler useless
9
10
// 1.ignore Exception; can't hook!
11
try {
12
$redis = new Redis();
13
$redis->connect('192.168.1.1',6379,1);
14
} catch(Exception $e) {
15
// don't call any Exception method
16
}
17
18
// 2.call any Exception method in catch
19
try {
20
$redis = new Redis();
21
$redis->connect('192.168.1.1',6379,1);
22
} catch(Exception $e) {
23
$e->getMessage();
24
}
25
26
// 3. throw without catch
27
class DivideByZeroException extends Exception {}
28
29
function throwException() {
30
Throw new DivideByZeroException("DivideByZeroException1");
31
}
32
33
//throwException();
34
35
// 4. throw with exception handler
36
// can NOT hook a exception_handler
37
38
class ExceptionHandler {
39
static $callback = array();
40
41
static function _exceptionHandler($e) {
42
var_dump('ExceptionHandler');
43
call_user_func(self::$callback, $e);
44
}
45
}
46
47
function exception_handler($exception) {
48
var_dump('exception_handler',$exception->getTraceAsString(),'exception_handler_end');
49
}
50
set_exception_handler('ExceptionHandler::_exceptionHandler');
51
52
aop_add_before('set_exception_handler()',function($jp){
53
$args = $jp->getArguments();
54
ExceptionHandler::$callback = $args[0];
55
$jp->setArguments(array('ExceptionHandler::_exceptionHandler'));
56
});
57
58
set_exception_handler('exception_handler');
59
//Throw new DivideByZeroException("DivideByZeroException3");
60
$redis->connect('192.168.1.1',6379,1);
61
$redis->ping();
62