thinkphp Function: D method

Source: Internet
Author: User
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:

    1. 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:

    1. 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

    1. 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:

    1. 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:

    1. Instantiate the UserService class $User = D (' User ', ' Service '); Instantiate the Userlogic class $User = D (' User ', ' Logic ');

复制代码

    1. D (' User ', ' Service ');

Copy Code

The lib/service/userservice.class.php is imported and instantiated, equivalent to the following code:

    1. import (' @. Service.userservice ');
      $User = new Userserivce (); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.