In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "what object is returned by the D method of thinkphp". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what object is returned by the D method of thinkphp" can help you solve the problem.
In thinkphp, the D method can return the instantiated model object. The D method is used to instantiate the custom model class, which is an encapsulation of Model class instantiation by the ThinkPHP framework. The syntax "D ('[project: / /] [grouping /] model', 'model layer name')" and the return value is the instantiated model object.
The operating environment of this tutorial: Windows7 system, thinkphp v5.1, Dell G3 computer.
D method should be used to instantiate custom model class, which is an encapsulation of Model class instantiation by ThinkPHP framework, and implements singleton pattern, which supports cross-project and group calls. The call format is as follows:
D ('[project: / /] [grouping /] model', 'model layer name')
The return value of the D method is the instantiated model object.
The D method can automatically detect the model class. If there is a custom model class, the custom model class is instantiated. If it does not exist, the Model base class is instantiated, and the instantiated model is not repeatedly de-instantiated.
The most common use of the D method is to instantiate a custom model for the current project, such as:
/ / instantiate the User model $User = D ('User')
The Lib/Model/UserModel.class.php file under the current project is imported, and then the UserModel class is instantiated, so the actual code may be equivalent to the following:
Import ('@ .Model.UserModel'); $User = new UserModel ()
The D method can support model instantiation across groups and projects, such as:
/ / instantiate User model D ('Admin://User') of Admin project / / instantiate User model D (' Admin/User') of Admin grouping
Since version 3.1, due to the addition of support for hierarchical models, the D method can also instantiate other models, such as:
/ / instantiate UserService class $User = D ('User','Service'); / / instantiate UserLogic class $User = D (' User','Logic'); D ('User','Service')
Lib/Service/UserService.class.php is imported and instantiated.
Expand knowledge: the main difference between D and M is that
The M method does not need to create a model class file, and the M method does not read the model class, so automatic validation is invalid by default, but it can be implemented by dynamic assignment.
The D method must have a create model class.
We can create a mapping object for a data table in two ways
First: $Test = D ('Test')
The second kind: $Test = new Model ('Test')
Although both of these can be used to select,insert,delete,udpate the data, in the
There is a big difference in data validation
Instantiating a model in the first way will have a data checking function. If title is not filled in, it will prompt "Please enter a title" (this is an automatic verification function provided by tp, of course, you also need to define the verification conditions in the corresponding model)
If you use the second one, you don't have this data validation feature, and you need to verify it manually.
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 that instantiated Model (the meaning is the same as the M () function).
And M only returns the object that instantiates Model. Its $name parameter handles operations on the database as the table name of the database.
To put it more popularly:
D is to instantiate a Model based on a Model file.
M instantiates a Model object dynamically by directly instantiating the Model method (the ThinkPHP base class), even if the corresponding Model file does not exist.
To put it more colloquially:
The M instantiation parameter is the table name of the database.
D instantiates the model file that you created under the Model folder.
D is when you do not define a model, the system automatically defines a model for you, so that you can do simple data input or output.
Every Action file should correspond to a Model file, if you define Model
For example: $Form = D ("User") can be changed to $Form = new UserModel (); (User is your model file name).
This is the end of the content about "what objects are returned by the D method of thinkphp". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.