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:
// instantiating 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 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:
// instantiate the user model of the admin project D (' Admin://user ')// instantiate the Admin group for User model 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:
// instantiate the UserService class $User = D (' User ', ' Service '); // instantiate the Userlogic class $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