thinkphp a method, r method, m method, D method Difference

Source: Internet
Author: User

In thinkphp, there are several ways to instantiate an object, if it is a class, there is a and r methods, the difference is that the A method is only an instantiation of the object, and the R method can be instantiated at the same time the method inside the object, where you need to specify, such as the following instance Code:

1<?PHP2 namespace admin\controller;3  usethink\controller;4 5 classGoodscontrollerextendscontroller{6      public functionshowlist () {7 8         //instantiating the controller a method9         $test=a ("Manager");Ten         Echo $test-test1 (); one  a         Echo"<br/>"; -  -         //You can also go across module calls the         $test _m=a ("home/goods"); -         Echo $test _m-Test (); -  -         Echo"<br/>"; +         //the R method can take the execution of the corresponding module -R ("home/goods/test"); +         //$this->display (); a     } at}

As can be seen from the above, the R method can also be used to Cross-module to Instantiate.

In addition, in model data models, it is necessary to use the following two methods, one is the D method, one is the M method, the former is the instantiation of the data model class, and the latter is the parent class of the instantiated data model.

1<?PHP2 namespace admin\controller;3  usethink\controller;4 classTestControllerextendscontroller{5      public functiontest1 () {6         $mythinkphp=m ("user");7         $t=$mythinkphp-Select ();8         Echo"<pre>";9         //var_dump ($mythinkphp->select ());Ten //echo Count ($t); one //echo "<br/>"; a //echo Count ($t [1]); - //echo "<br/>"; - //for ($i =0; $i <count ($t); $i + +) { the //if ($t [$i] [' name ']== ' xuning ') { - //echo "exists for this user, user ID is". ($i-1); -         //         } -         // } + //echo "<br/>"; - //this completes the data traversal from the database +         Echo"<center>"; a         Echo"; at         Echo"<table border= ' 1px ' >"; -          for($i= 0;$i<Count($t);$i++){ -             Echo"<tr>"; -                     Echo"<td>"; -                     Echo $t[$i[' ID ']; -                     Echo"</td>"; in                     Echo"<td>"; -                     Echo $t[$i[' name ']; to                     Echo"</td>"; +                     Echo"<td>"; -                     Echo $t[$i[' Password ']; the                     Echo"</td>"; *             Echo"</tr>"; $         }Panax Notoginseng         Echo"</table>"; -         Echo"</center>"; the         Echo"</pre>"; +         $t 2=d ("test_1"); a         $temp=$t 2-Select (); the         Echo $temp[0] [' name ']; +     } -      public functiontest2 () { $         //in fact, the instantiation here is to build the database object, not accurate to the table, at the same time $ //we can also not build a data model to use directly, so, -         $t=New\model\testmodel; -Show_bug ($t); the     } -      public functiontest3 () {Wuyi         $user=d ("Test"); theShow_bug ($user); -     } wu}

Although there is no difference from usage, the D method instantiates only the defined data model class, but the M method instantiates the parent class of the data model.

Reference http://blog.csdn.net/mycodedream/article/details/45340949

thinkphp a method, r method, m method, D method Difference

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.