Home >PHP Framework >ThinkPHP >Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

尚
forward
2020-05-20 09:09:053230browse

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

ThinkPHP supports two construction methods: __construct and _initialize (ThinkPHP's built-in construction method).

Modify the user controller class file UserController.class.php as follows:

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

1. __construct constructor method

Modify the middle The controller class CommonController.class.php is as follows:

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

Note: empty() supports expressions after version 5.5, otherwise the following error will be reported:

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

The running results are as follows:

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

The intermediate controller must first construct the parent class before it can use the parent class's methods. Modify the code as follows:

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

Run result:

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

2. _initialize construction method, as follows:

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

The above effects can also be achieved through the _initialize construction method. There is no need to construct a parent class. View the base class controller code as follows:

Introduction to the difference between __construct and _initialize when thinkingphp login restrictions

Through Controller.class.php It can be seen from the architecture function that as long as the _initialize method exists, it will be directly called to initialize the controller, so ThinkPHP's built-in

construction method does not need to construct the parent class.

Recommended tutorial: "TP5"

The above is the detailed content of Introduction to the difference between __construct and _initialize when thinkingphp login restrictions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete