This article is about the thinkphp function in detail: D method, has a certain reference value, now share to everyone, the need for friends can refer to
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, if it does not exist,
Real
The model base class is instantiated, and the instantiated models are not repeated.
The most common use of the D method is to instantiate a custom model of the current project, for example:
Instantiate 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 for the Admin Project D (' Admin://user ')//Instantiate the Admin group for user Model 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:
Instantiate the UserService class $User = D (' User ', ' Service '); Instantiate the Userlogic class $User = D (' 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 ();