This article mainly introduces the use of the d method of ThinkPHP3.1. If you need a friend, you can refer to the D method, which should be a lot of methods used to instantiate a custom model class, it is an encapsulation of the Model class instantiated by the ThinkPHP framework and implements the singleton mode. It supports cross-project and group calls. The call format is as follows:
D ('[Project: //] [group/] model', 'model layer name ')
The method returns an instantiated model object.
The D method can automatically detect Model classes. If a custom Model class exists, the user-defined Model class is instantiated. If the class does not exist, the Model base class is instantiated, at the same time, models that have already been instantiated will not be instantiated repeatedly.
D. The most common method is to instantiate a custom model of the current project. For example:
// Instantiate the User model $ User = D ('user ');
The Lib/Model/UserModel. class. php file under the current project will be imported, and then the UserModel class will be instantiated. Therefore, the actual code may be equivalent to the following:
import('@.Model.UserModel');$User = new UserModel();
However, if the D method is used, if the UserModel class does not exist, it will automatically call
new Model('User');
In addition, you do not need to instantiate the object again during the second call, which can reduce the overhead of Object Instantiation.
The D Method supports cross-group and project instantiation models, for example:
// Instantiate the User model D ('admin: // user') of the Admin project // instantiate the User model D ('admin/user') of the Admin group ')
Note: To Call The model across projects, make sure that the directory structure of the two projects is parallel.
ThinkPHP version 3.1 and later versions. Due to the added support for layered models, the D method can also instantiate other models.For example:
// Instantiate UserService class $ User = D ('user', 'service'); // instantiate UserLogic class $ User = D ('user', 'lock ');
D ('user', 'service'); imports Lib/Service/UserService. class. php and instantiates it, which is equivalent to the following code:
import('@.Service.UserService');$User = new UserSerivce();