Home  >  Article  >  PHP Framework  >  thinkphp D method returns what

thinkphp D method returns what

青灯夜游
青灯夜游Original
2022-03-15 18:51:271654browse

In thinkphp, the D method can return the instantiated model object. The D method is used to instantiate a custom model class. It is an encapsulation of the Model class instantiation by the ThinkPHP framework. The syntax is "D('[project://][group/]model','model layer name')", The return value is the instantiated model object.

thinkphp D method returns what

The operating environment of this tutorial: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.

D method should be the most commonly used method. It is used to instantiate custom model classes. It is an encapsulation of the Model class instantiation by the ThinkPHP framework. It also implements the singleton mode and supports cross-projects. And grouped calls, the calling format is as follows:

D('[项目://][分组/]模型','模型层名称')

The return value of the D method is the instantiated model object.

D method can automatically detect the model class. If a custom model class exists, the custom model class will be instantiated. If it does not exist, the Model base class will be instantiated. At the same time, for the instantiated The model passed will not be instantiated repeatedly. The most common usage of the

D method is to instantiate a custom model of the current project. For example:

// 实例化User模型
$User = D('User');

will import the Lib/Model/UserModel.class.php file under the current project, Then instantiate the UserModel class, so the actual code may be equivalent to the following:

import('@.Model.UserModel');
$User = new UserModel();

D method can support instantiating models across groups and projects, for example:

//实例化Admin项目的User模型
D('Admin://User')
//实例化Admin分组的User模型
D('Admin/User')

Starting from version 3.1 , due to the added support for hierarchical models, the D method can also instantiate other models, for example:

// 实例化UserService类
$User = D('User','Service');
// 实例化UserLogic类
$User = D('User','Logic');
D('User','Service');

will import Lib/Service/UserService.class.php and instantiate it.

Extended knowledge: The main difference between D and M is that

The M method does not need to create a model class file. The M method does not read the model class, so by default Automatic verification is invalid, but it can be achieved through dynamic assignment

And the D method must create a model class.

We can use the following two methods to create a mapping object of a data table

The first one:$Test = D('Test')

Second type: $Test = new Model('Test')

Although both of these can perform select, insert, delete, and udpate operations on data, in

There is a big difference in data verification.

Using the first method to instantiate a model will have a data checking function. If the title is not filled in, it will prompt "Please enter the title" (this is tp Provides an automatic verification function, of course the verification conditions need to be defined in the corresponding model);

If you use the second type, you will not have this data verification function, and you need to manually verify it.

The summary is as follows:

  • The D function instantiates the module under the Lib/Model of your current project.

  • If the module does not exist, directly return the object of the instantiated Model (the meaning is the same as the M() function).

  • And M only returns the object that instantiates the Model. Its $name parameter is used as the table name of the database to handle operations on the database.

In layman’s terms:

  • D is to instantiate a Model based on a Model file.

  • M dynamically instantiates a Model object by directly instantiating the Model method (ThinkPHP base class), even if the corresponding Model file does not exist.

To put it more simply:

  • M instantiation parameter is the table name of the database.

  • D instantiates the model file you created under the Model folder.

  • D means that when you do not define a model, the system automatically defines a model for you so that you can perform simple data input or output.

  • Each Action file should correspond to a Model file. If you define a Model,

For example: $Form = D("User") can be changed to $Form = new UserModel(); (User refers to your model file name).

[Related tutorial recommendations: thinkphp framework]

The above is the detailed content of thinkphp D method returns what. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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