Starting from this article, we will give you a detailed description of the use of the framework built-in functions. D method
The D method should be used in a more numerous way, to instantiate a custom model class, is a thinkphp framework for the model class instantiation of a package, and implemented a singleton mode, support cross-project and group calls, the invocation format is as follows:
D (' [Project://][Group/] model ', ' model layer name ')
The return value of the method is the instantiated model object.
The D method automatically detects the model class and, if there is a custom model class, instantiates the custom model class and, if not, instantiates the model base class, and does not repeat the instantiation for the instantiated models.
The most common use of the D method is to instantiate a custom model of the current project, for example:
- // 实例化User模型
- $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 might be equivalent to the following:
- import(‘@.Model.UserModel‘);
- $User = new UserModel();
复制代码
But if you use the D method, if the Usermodel class does not exist, it is automatically called
- new Model(‘User‘);
复制代码
And the second call without re-instantiation, you can reduce the cost of some object instantiation.
The D method can support cross-grouping and project instantiation models, such as:
- //实例化Admin项目的User模型
- D(‘Admin://User‘)
- //实例化Admin分组的User模型
- D(‘Admin/User‘)
复制代码
Note: To implement a cross-project invocation model, you must ensure that the directory structure of the two projects is tied.
Beginning with version 3.1, the D method can also instantiate other models because of the increased support for the layered model, for example:
- // 实例化UserService类
- $User = D(‘User‘,‘Service‘);
- // 实例化UserLogic类
- $User = D(‘User‘,‘Logic‘);
复制代码
- D(‘User‘,‘Service‘);
复制代码
The lib/service/userservice.class.php is imported and instantiated, equivalent to the following code:
- import(‘@.Service.UserService‘);
- $User = new UserSerivce();
thinkphp Function: D method