Home > PHP Framework > ThinkPHP > body text

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

Release: 2020-05-20 09:09:05
forward
3092 people have browsed it

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!