SoFunction
Updated on 2025-03-10

Overview of new features of ThinkPHP3.1.3 version

ThinkPHP3.1.3 version has some features, which are worth paying attention to. Let’s briefly talk about it below.

1. Improvements in exceptions

The new version of ThinkPHP3.1.3 rewrites the exception class ThinkException (actually it is completely simplified to directly inherit the system Exception class), and encapsulates the exception logic into the Think class. It mainly involves the appException method and halt function of the Think class.
In addition, the improved exception handling supports the capture of fatal errors in the system. The Think class has added a fatalError method, and the principle is to use it.

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

Therefore, it can make the system fatal errors friendly prompts through the unified exception template interface.

2. Support for PDO parameter binding

Because the ThinkPHP3.* version uses a hybrid database driver and also supports PDO method, but the previous version did not optimize for PDO, it was just a simple encapsulation. 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).

There are two types of support for parameter binding operations in the system:Automatic binding and manual binding

Automatic bindingFor write operations (including the addition and update of data here), the framework will automatically convert the relevant data into parameter binding mode to execute. This part does not require additional processing, because SQLSRV can only support UTF8 data writing by passing values ​​through parameter binding. However, if manual parameter binding is required for each write data, it will be more troublesome. In order to avoid conflicts with manual parameter binding, automatic parameter binding adopts the method of encoding the field name md5.

Manual binding, usually used for querying conditions, and adopts bind coherent operation methods, such as:

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


3. Add a safe way to obtain variables

The previous version used the Action class's _post _get and other methods to securely obtain variables. Although it is no problem, the limitation is that it can only obtain variables in the controller. The new version independently sets this function into a shortcut method I, which can be used anywhere.
How to use it is as follows:

I('',0); // Get $_GET['id'] if it does not exist, defaults to 0I('','','htmlspecialchars'); // Get $_POST['name'] Filter using htmlspecialchars methodI('id'); // Get id parameters to automatically judge get or postI(''); // Get id parameters and automatically determine that get or post is equivalent to the above usageI(''); // Get the id parameter of the put request

It also supports getting the entire array, for example:

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

In the case of using the I method, the system's VAR_FILTERS and DEFAULT_FILTER filtering configurations are still valid.

4. Multiple calls to the 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 multiple where conditions are used, the subsequent conditions will be combined with the previous conditions, and the final conditions are 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 it in the controller:

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

or:

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