The difference between the ThinkPHP3.2.3 m function and the D function

Source: Internet
Author: User
Tags array definition explode

1. Direct instantiation:

The following code is: the constructor in/think/model, which is the model class under the Think namespace.

Three parameters: Model name + table Prefix + database connection information

Model name + Table prefix: Mates are used together to automatically identify the name of the data table.

Database connection Information: Database connection information for the current data table. There are three ways to define it: A string definition, an array definition, and a configuration definition.

The table prefix is null, converted to ', in the use of the table prefix in the configuration file, and not ', using the current table prefix.

For example: $User = new \home\model\usermodel (); Instantiate the Usermodel class under the Home\model namespace and the corresponding table name is Think_user

$New = new \home\model\newmodel (' new ', ' Ln_ ', $connection); Instantiating the Newmodell class under the Home\model namespace and the distinguished table named Ln_new

$New = New \home\model\newmodel (' User ', ' ln_ ', $connection); Instantiating the Newmodell class under the Home\model namespace and the distinguished table named Ln_user

Note: The namespace address of the class is consistent with the path address where it resides.

 Public function__construct ($name= ",$tablePrefix= ",$connection= ' ') {        //Model Initialization        $this-_initialize (); //Gets the model name, which has two forms: a model name and a database name. Model Name        if(!Empty($name)) {            if(Strpos($name,‘.‘)) {//support database name. Definition of model name                List($this->dbname,$this->name) =Explode(‘.‘,$name); }Else{                $this->name =$name; }        }ElseIf(Empty($this-name)) {            $this->name =$this-Getmodelname (); }        //Set Table Prefixes        if(Is_null($tablePrefix)) {//prefix null means no prefix            $this->tableprefix = ' '; }ElseIf('! =$tablePrefix) {            $this->tableprefix =$tablePrefix; }ElseIf(!isset($this-Tableprefix)) {            $this->tableprefix = C (' Db_prefix '); }        //database initialization operation//Get Database Operation object//The current model has separate database connection information        $this->db (0,Empty($this->connection)?$connection:$this->connection,true); }

2.M function instantiation

The M function instantiation parameter is the same as the direct instantiation. The default is to instantiate the model class file under \think\model.

Of course, you can also instantiate other public model classes, such as:

$User = M (' \home\model\commonmodel:user ', ' ln_ ', $connection);

Equivalent to $user = new \home\model\commonmodel (' User ', ' ln_ ', $connection); The table name corresponding to the Commonmodel model class is ln_user.

functionM$name= ",$tablePrefix= ",$connection= ' ') {    Static $_model=Array(); if(Strpos($name,‘:‘)) {        List($class,$name)    =Explode(‘:‘,$name); }Else{        $class= ' Think\\model '; }    $guid=   (Is_array($connection)?implode(‘‘,$connection):$connection).$tablePrefix.$name. ‘_‘ .$class; if(!isset($_model[$guid]))        $_model[$guid] =New $class($name,$tablePrefix,$connection); return $_model[$guid];}

3.D function Instantiation: Two parameters: Model name + model layer Name

1) used to instantiate a custom model class.

For example: $User = D (); Equivalent to $User = new \think\model ();

$User = D (' User ');  Equivalent to $User = new \home\model\usermodel (); Using the default module (default_module)

$User = D (' User ', ' model_1 '); Equivalent to $User = new \home\model_1\usermodel ();

Note: The above instantiation assumes that the default module (default_module) has the corresponding model class file in the model layer in home. When the model class file does not exist in the model layer of the default module, the D function attempts to instantiate

The model class file in the model layer under the public module (Common). For the second item in the above example: non-existent, then $user = new \common\model\usermodel ();

2) for automatic detection of model classes. If a custom model class exists, the custom model class is instantiated, and if it does not exist, the \think\model base class of the system is instantiated, and the model that has been instantiated is not repeated

instantiated.

3) Support cross-module instantiation: Model classes that automatically load public modules are not supported when instantiating model classes across modules.

For example: D (' Admin/user '); Instantiate the Usermodel model class in the Admin module

D (' Extend://editor/info); Instantiate extend extension Infomodel model class under clear space

functionD$name= ",$layer= ' ') {    if(Empty($name))return NewThink\model; Static $_model=Array(); $layer=$layer? : C (' Default_m_layer '); if(isset($_model[$name.$layer]))        return $_model[$name.$layer]; $class= Parse_res_name ($name,$layer);//return: Default module \ Default model layer name is Default_module\default_m_layerif(class_exists($class)) {        $model=New $class(basename($name)); }ElseIf(false===Strpos($name,‘/‘)){        //automatically load the model below the public module        if(! C (' App_use_namespace ') {import (' common/'.$layer.‘ /‘.$class); }Else{            $class= ' \\Common\\ '.$layer.‘ \\‘.$name.$layer; }        $model=class_exists($class)?New $class($name) :NewThink\model ($name); }Else{think\Log:: Record (' d method instantiation did not find Model class ').$class, Think\Log::NOTICE); $model=NewThink\model (basename($name)); }    $_model[$name.$layer]  =$model; return $model;}

The difference between the ThinkPHP3.2.3 m function and the D function

Related Article

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.