Tag Archives: exception

PHP备忘

1,stdClass 他是php内置的一个类,提供给我们直接实例化使用。 $obj = new stdClass();$obj->prop = ‘hello world’;echo $obj->prop; 我们可以看看他的内部结构 Reflection::export(new ReflectionClass(‘stdClass’));/* 输出结果Class [ <internal> class stdClass ] {  – Constants [0] {  }  – Static properties [0] {  }  – Static methods [0] {  }  – Properties [0] {  }  – Methods [0] {  }}</internal>*/ 2,php的exception和error处理 他们各自的发生: exception可以通过php5 的try{}抛出,然后通过catch{}被捕获。 php内置函数执行时发生问题,是通过trigger_error显示error。 使用异常: <?php// 定义未捕获异常的处理函数function [...]

PHP中把Exception写入到日志

PHP5中把Exception写入到日志,是比较好的方法,开发时可以随时查看,部署后也不用担心在页面上打印出一对信息造成不友好的体验和安全隐患。代码: <?php error_reporting(E_ALL); function exceptionLogger($exception) {        $file = ‘exceptionLog.log’;    file_put_contents($file,$exception->__toString(),FILE_APPEND);    echo "Sorry!I’m Sick…";} set_exception_handler(‘exceptionLogger’); function connectToDatabase() {     if(!$conn = @mysql_connect(‘localhost’, ‘root’, ”)) {        throw new Exception;    }} connectToDatabase();?>

Page 1 of 1 1