ThinkPHP version 3.1.3 has some features that are worthy of attention. Let’s briefly talk about them below.
1. Improvements in exceptions
The new version of ThinkPHP3.1.3 rewrites the exception class ThinkException (in fact, it is completely simplified to directly inherit the system Exception class), and improves the exception logic and encapsulates it into the Think class. It mainly involves the appException method and halt function of the Think class.
And the improved exception handling supports the capture of system fatal errors. The Think class adds a fatalError method. The principle is to use
register_shutdown_function(array('Think','fatalError'));
This allows system fatal errors to be prompted in a friendly manner through a unified exception template interface.
2. Support for PDO parameter binding
Because the ThinkPHP3.* version uses a hybrid database driver and also supports PDO mode, but the previous version was not optimized for PDO, but was simply encapsulated. Version 3.1.3 improves support for PDO and Sqlarv, because both PDO and sqlsrv support parameter binding operations (note that databases and drivers that do not support parameter binding cannot use the parameter binding function).
The system supports two types of parameter binding operations: automatic binding and manual binding.
Auto binding is for write operations (including adding and updating data). The framework will automatically convert the relevant data into parameter binding mode for execution. This part does not require additional processing because sqlsrv can only support UTF8 data writing by passing values through parameter binding. However, it will be more troublesome if manual parameter binding is used every time the data is written. In order to avoid conflicts with manual parameter binding, automatic parameter binding uses md5 encoding of field names.
Manual binding is usually used for query conditions and the like, and the bind coherent operation method is used, for example:
$model->where(array('id'=>':id','name'=>':name'))->bind(array(':id'=>$id,':name'=>$name))->select();
3. Add safe variable acquisition method
The previous version used methods such as _post_get of the Action class to safely obtain variables. Although there is no problem, the limitation is that variables can only be obtained in the controller. The new version separates this function into a shortcut method I, which can be used Anywhere.
How to use:
I('get.id',0); // 获取$_GET['id'] 如果不存在则默认为0 I('post.name','','htmlspecialchars'); // 获取$_POST['name'] 采用htmlspecialchars方法过滤 I('id'); // 获取id参数 自动判断get或者post I('param.id'); // 获取id参数 自动判断get或者post 和上面用法等效 I('put.id'); // 获取put请求的id参数
also supports getting the entire array, for example:
I('get.'); // 获取$_GET数组 I('post.'); // 获取$_POST数组
When using the I method, the system's VAR_FILTERS and DEFAULT_FILTER filtering configurations are still valid.
4. Multiple calls of where method
The where method of the model class can support multiple calls in array mode, for example:
$model->where(array('a'=>1,'c'=>2))->where(array('a'=>3,'b'=>1))->select();
When there are multiple where conditions, the later conditions will be merged into the previous conditions, and the final condition is equivalent to:
$model->where(array('a'=>3,'b'=>1,'c'=>2))->select();
5. The assign method in the controller supports coherent operations
We can use in the controller:
$this->assign('name',$name)->assign('email',$email)->display();
or:
$this->assign(array('name'=>$name,'email'=>$email))->display();