The difference between M and D function in thinkphp

Source: Internet
Author: User
The difference between M and D functions in thinkphp
Reference Source: http://blog.csdn.net/siren0203/archive/2010/10/29/5974415.aspx

Two have something in common. are used to instantiate objects:

But the two are different when used ... Why??

First look at the code

function M

function M ($name = ', $class = ' Model ')   {       static $_model = Array ();       if (!isset ($_model[$name. '_' . $class])           //default is the  new Model ($name)           $_model[$name. '_' . $class] = new $class ($name);           Returns a $name of the instantiated object       return $_model[. '_' . $class];   }  


$_model[$name. '_' . $class] = new $class ($name);

The default is the new Model ($name)

Other words MThe object returned is the model

If $name is not empty then the meaning of $name is the name of the table representing the database (this is said later)

A model object is instantiated dynamically by instantiating the model method directly (Thinkphp base class). even if the corresponding model file does not exist

and look at the D function .
function D ($name = ", $app =") {Static $_model = array ();        if (empty ($name))//$name null action is equivalent to function m ();    return new Model;    if (empty ($app))//default module name is defined in common/convention.php =@ $app = C (' Default_app ');    If an object already exists, return directly to the IF (Isset ($_model[$app. $name])) return $_model[$app. $name];    $OriClassName = $name;    If the $name parameter is passed with @.**. Module name, if (Strpos ($name, '. '))        {$array = explode ('. ', $name);        $name = module Name $name = Array_pop ($array); $className = $name.        ' Model '; Import the file where the model module is imported ($app. '. Model. '. Implode ('. ', $array). '.' .    $className); }//import directly else {$className = $name.        ' Model '; Import ($app. '. Model. '.    $className);    }//Instantiate object returns if (Class_exists ($className)) {$model = new $className ();    } else {$model = new model ($name);    } $_model[$app. $OriClassName] = $model; return $model;} 

The specific comments are already in the code.

In contrast, we should be able to see ...

the D function instantiates theLib/model the following module for your current project

Returns the object of the instantiated model directly if the module does not exist (meaning is the same as the M () function)

and M returns only the object that instantiated the model. Its $name parameter handles the operation of the database as the table name of the database

Popular Point, D is to instantiate a model file based model, and M is a direct instantiation of the model method (thinkphp base class) to dynamically instantiate a model object, even if the corresponding model file does not exist.

In a more popular sense, it is:
the M instantiation parameter is the table name of the database.
d instantiates a model file that you set up under the Models folder yourself.

1/F Ninedoors 2011-10-23

This man, there is a problem, it feels very strange, but do not know why.
When I use the thinkphp framework to get the data encoding in the database and return the XML,
found that if the acquisition of the database using the D method, the output of the XML in front of a few more spaces, this does not know why.
Instead of using the M method, the space is gone.
Hope to advise.
[Email protected]

2 floor again every landscape 2011-10-31

Ninedoors wrote

This man, there is a problem, it feels very strange, but do not know why.
When I use the thinkphp framework to get the data encoding in the database and return the XML,
found that if the acquisition of the database using the D method, the output of the XML in front of a few more spaces, this does not know why.
Instead of using the M method, the space is gone.
Hope to advise.
[Email protected]


This is not the case, but you can filter out these spaces with functions such as trim or regular expressions.

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