Overview of new features in ThinkPHP version 3.1. 3

  • 2021-07-02 23:36:49
  • OfStack

ThinkPHP version 3.1. 3 has a few features, which are worth paying attention to. Let's briefly talk about them below.

1. Improvement in anomalies

The new version of ThinkPHP 3.1. 3 rewrites the exception class ThinkException (in fact, it is completely simplified to directly inherit the system Exception class), and encapsulates the exception logic improvement into the Think class. It mainly involves appException method and halt function of Think class.
And the improved exception handling supports the capture of fatal errors in the system. Think class adds fatalError method, and the principle is to use


register_shutdown_function(array('Think','fatalError'));

Therefore, the fatal error of the system can be friendly prompted through the exception template interface of Unification 1.

2. Support for PDO parameter binding

Because ThinkPHP3.* version adopts hybrid database driver and supports PDO mode, the previous version did not optimize PDO, but 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 parameter binding functionality).

The system supports two kinds of parameter binding operations: automatic binding and manual binding.

Automatic binding is for write operations (including data additions and updates here), The framework will automatically convert the relevant data into parameter binding mode for execution, and this part does not need extra processing, because sqlsrv can only support UTF8 data writing by using parameter binding mode, but it will be troublesome to use manual parameter binding every time writing data. In order to avoid conflict with manual parameter binding, automatic parameter binding adopts the way of encoding the field name md5.

Manual binding, usually used for query criteria and the like, and using bind coherent operation methods, such as:


$model->where(array('id'=>':id','name'=>':name'))->bind(array(':id'=>$id,':name'=>$name))->select();


3. Add a variable security acquisition method

In the previous version, variables are obtained safely by _ post _ get of Action class. Although there is no problem, the limitation is that variables can only be obtained in the controller. In the new version, this function is separated into a shortcut I, which can be used anywhere.
The usage method is as follows:


I('get.id',0); //  Get $_GET['id']  If it does not exist, it defaults to 0
I('post.name','','htmlspecialchars'); //  Get $_POST['name']  Adopt htmlspecialchars Method filtering 
I('id'); //  Get id Parameter   Automatic judgment get Or post
I('param.id'); //  Get id Parameter   Automatic judgment get Or post  Equivalent to the above usage 
I('put.id'); //  Get put Requested id Parameter 

Getting the entire array is also supported, for example:


I('get.'); //  Get $_GET Array 
I('post.'); //  Get $_POST Array 

The VAR_FILTERS and DEFAULT_FILTER filtering configurations of the system are still valid when the I method is used.

4. Multiple calls to the where method

The where method of the model class can support multiple calls in array mode, such as:


$model->where(array('a'=>1,'c'=>2))->where(array('a'=>3,'b'=>1))->select();

When there are multiple where conditions, the following 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 operation

We can use in the controller:


$this->assign('name',$name)->assign('email',$email)->display();

Or:


$this->assign(array('name'=>$name,'email'=>$email))->display();


Related articles: