清风徐来
知是行之始,行是知之成

PHP备忘

PHP exception异常ANDerrorORstdClasstip 约 3 分钟

1,stdClass 他是php内置的一个类,提供给我们直接实例化使用。 $obj = new stdClass(); $obj->prop = ‘hello world’; echo $obj->prop; 我们可以看看他的内部结构 Reflection::export(new ReflectionClass(‘stdClass’)); /* 输出结果 Class [ class stdClass ] {

  • Constants [0] { }
  • Static properties [0] { }
  • Static methods [0] { }
  • Properties [0] { }
  • Methods [0] { } } */ 2,php的exception和error处理 他们各自的发生: exception可以通过php5 的try{}抛出,然后通过catch{}被捕获。 php内置函数执行时发生问题,是通过trigger_error显示error。 使用异常:
Exception:",$e->getMessage(),"
"; echo "Stack Trace String:".$e->getTraceAsString(); } // 设置用户自定义异常处理函数 set_exception_handler('ExceptionHandler'); throw new Exception('Uncaught Exception occurred.'); echo 'Not Executed.' ?>
使用错误处理: Custom Error:$errmsg
n"; $msg .= "File:$errfile
n"; $msg .= "Line Number:$errline
n"; } echo $msg; // 记录错误信息 error_log(date("[Y-m-d H:i:s]")." -[".$_SERVER['REQUEST_URI']."] :
n".$msg."
", 3, 'error_log.html'); //exit(); }

// set_error_handler()函数用于让用户自定义错误处理函数 // set_error_handler(error_function, error_type) // error_function 必须, 制定发生错误时运行的函数 // error_type 可选, 规定不同的错误级别提示的不同信息, 默认是"E_ALL" set_error_handler(‘ErrorHandler’);

$foo = 2; if ($foo > 1) { // trigger_error()接收一个错误信息和一个常量作为参数, // 常量为 E_USER_ERROR -> a fatal error // E_USER_WARNING -> a non-fatal error // E_USER_NOTICE -> a report that may not represent an error trigger_error(“A custom error has been trigglered”, E_USER_ERROR); } echo ‘Go on…’; 不同之处,处理异常后,脚本不再执行;但是error有可能会继续执行。 整理一下,可以放到tinymvc里做script plugin。

Exception:",$e->getMessage(),"
n"; echo "Stack Trace String:".$e->getTraceAsString(); } // 设置用户自定义异常处理函数 set_exception_handler('ExceptionHandler'); function ErrorHandler($errno, $errmsg, $errfile, $errline){ if($errno == E_USER_ERROR) { $msg = "Custom Error:$errmsg
n"; $msg .= "File:$errfile
n"; $msg .= "Line Number:$errline
n"; } echo $msg; // 记录错误信息 error_log(date("[Y-m-d H:i:s]")." -[".$_SERVER['REQUEST_URI']."] :
n".$msg."
", 3, 'error_log.html'); //exit(); } // set_error_handler()函数用于让用户自定义错误处理函数 // set_error_handler(error_function, error_type) // error_function 必须, 制定发生错误时运行的函数 // error_type 可选, 规定不同的错误级别提示的不同信息, 默认是"E_ALL" set_error_handler('ErrorHandler'); /* * DEOM * * throw new Exception('Uncaught Exception occurred.'); * * $foo = 2; * * if ($foo > 1) { * // trigger_error()接收一个错误信息和一个常量作为参数, * // 常量为 E_USER_ERROR -> a fatal error * // E_USER_WARNING -> a non-fatal error * // E_USER_NOTICE -> a report that may not represent an error * trigger_error("A custom error has been trigglered", E_USER_ERROR); * } * */ ?>
3,include 创建配置文件 include这个语法,也可以这样用的。比如有个config.db.php 'localhost', 'database' => 'db', 'username' => 'root', 'password' => '123456', ); 这个include可以直接赋值给变量: $config = include 'config.db.php'; // $config现在就是这个数组 // echo $config['database']; 4,简单的判断用OR , AND mysql_connect('locahost','root','') OR die('数据库连接失败'); 之所以用 OR 可以达到,当函数错误后,执行OR后面的函数是因为: 1.逻辑运算的运算顺序是从左向右 2.OR运算中,如果运算时有值为真了,则整个表达式为真,后面不需要运算 前面执行成功就不用执行后面的语句时用OR;只有前面执行成功了,才执行后面的语句时,用AND.如: mysql_connect('locahost','root','') AND echo '数据库连接成功'; 一般需要给后面语句赋值并要保持程序运行的情况,用AND。