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