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:
- Instantiating the user model
- $User = D(' User ');
Copy Code
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();
Copy Code
But if you use the D method, if the Usermodel class does not exist, it is automatically called
- New Model(' User ');
Copy Code
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:
- Instantiate the user model of the admin project
- D(' Admin://user ')
- //Instantiate user model for admin group
- D(' Admin/user ')
Copy Code
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:
/ / instantiating the UserService class
$User =< Span class= "PLN" > d ( ' User ' ,
Instantiation Userlogic class
$User = d ( span class= "str" > ' User ' , ' Logic '
复制代码
- D(' User ',' Service ');
Copy Code
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