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:
1. __construct constructor method
Modify the middle The controller class CommonController.class.php is as follows:
Note: empty() supports expressions after version 5.5, otherwise the following error will be reported:
The running results are as follows:
The intermediate controller must first construct the parent class before it can use the parent class's methods. Modify the code as follows:
Run result:
2. _initialize construction method, as follows:
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:
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!