The difference between thinkphp5 and 3
First of all, let me state that this chapter is not a guide to upgrade old projects to 5.0, but is for developers using version 3.X to get familiar with and get started with this new version faster. At the same time, it is also strongly recommended that developers abandon their old thinking patterns, because 5.0 is a brand new subversive and reconstructed version.
3.X old ideas that need to be abandoned
Changes in URL
First of all We apologize for the incorrect guidance that the laxity of . Those that do not belong to $_GET can now be obtained through 'param'. The specific use can be queried through the request part.
Model changes
The new version of the model query returns the default 'object', and the system adds the 'toArray' method by default. Many developers use 'all' or 'select' 'Try to use 'toArray' to convert to an array. I hope developers can understand the concept of 'object', try to use 'object' to use data, or use the 'db' method to operate the database, and also remind you of this part' For developers who abuse 'toArray', the result of 'all' or 'select' is an array collection of objects, which cannot be converted using 'toArray'.
New version changes
Naming convention
Directory and file names use 'lowercase underscore', And start with a lowercase letter; class libraries and function files are uniformly suffixed with .php; class file names are defined in namespaces, and the path of the namespace is consistent with the path of the class library file (including upper and lower case); class names and class files The names should be consistent, and uniformly use camel case naming (the first letter is capitalized)
Function
The system no longer relies on any functions, but only provides assistants for commonly used operation encapsulation Function; the single-letter function is abandoned, and the system loads the assistant function by default. For details, please refer to the previous chapter 'Assistant Function';
routing
5.0 URL access no longer supports ordinary URL mode and routing do not support regular routing definitions. Instead, they are all changed to rule routing combined with variable rules (regular definitions). The details will not be described here.
Controller
The namespace of the controller has been adjusted, and there is no need to inherit any controller class.
The namespace of the application class library is unified as app (modifiable) instead of module name; the class name of the controller does not have the Controller suffix by default. You can configure the controller_suffix parameter to enable the controller class suffix; the controller operation method Use the return method to return data instead of direct output; abolish the original pre- and post-operation methods;
Version comparison
3.2 version Controller writing method
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { public function hello() { echo 'hello,thinkphp!'; } }
5.0 version controller writing method
namespace app\index\controller;class Index { public function index() { return 'hello,thinkphp!'; } }
3.2 version controller naming
IndexController. class.php
5.0 version controller naming
Index.php
**Correct output template in controller**
5.0 outputs the template in the controller. The usage method is as follows:
If you inherit think\Controller, you can use:
return $this->fetch('index/hello');
If your controller If you do not inherit think\Controller, use:
return view('index/hello');
Model
If you have to compare the improvements with the old version, the model is divided into databases The three parts, model and validator, respectively correspond to M method, model and automatic verification, and they have all been strengthened. A brief introduction is given below.
Database
The database query function of 5.0 has been enhanced. The chain query that originally needed to be used through the model can be called directly through the Db class. The M function call can use the db function instead, for example:
3.2 version
M('User')->where(['name'=>'thinkphp'])->find();
5.0 version
db('User')->where('name','thinkphp')->find();
Model
The new version of model query adds static methods, for example:
User::get(1); User::all();User::where('id','>',10)->find();
The model part has enhanced many functions, please refer to the "Model Chapter" for details.
Automatic verification
Compared with the old version, it can be understood as the previous automatic verification and different from the previous verification;
ThinkPHP5.0 verification uses an independent \think\Validate class or validator for verification. It is not only applicable to models, but can also be called directly in the controller. Please refer to the "Verification" chapter for specific usage rules, which will not be described here.
Configuration file
The new version has many configuration parameters or configuration levels that are different from before. It is recommended that you either take a look at the code, or Read the official development manual carefully and don't waste a whole day on configuration issues.
abnormal
5.0 has zero tolerance for errors. By default, exceptions will be thrown for any level of error, and the exception page has been redesigned to display detailed error information for easy debugging.
Abandonment of system constants
Compared with previous versions, the 5.0 version has made a large number of discards of system changes. If users have relevant needs, they can do so by themselves. Definition
The following are the abolished constants
REQUEST_METHOD IS_GET IS_POST IS_PUT IS_DELETE IS_AJAX __EXT__ COMMON_MODULE MODULE_NAME CONTROLLER_NAME ACTION_NAME APP_NAMESPACE APP_DEBUG MODULE_PATH etc.
Some constants can be obtained in Request, please refer to " Request a Chapter".
Note: Once again, this chapter is only written for developers who have previously used version 3.X to quickly understand 5.0. The specific functions of 5.0 require developers to read the manual thoroughly.
Assistant function
The comparison between the 5.0 assistant function and the single-letter function of version 3.2 is as follows:
C config
E exception
G debug
L lang
T abolish
I input
N abolish
D model
M db
A controller
R action
B abolish
U url
W widget
S cache
F Abolish
For more ThinkPHP related technical articles, please visit the ThinkPHP Usage Tutorial column to learn!
The above is the detailed content of The difference between thinkphp5 and 3. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.
