You can use $ this to call methods in the current module, but in many cases, the methods of other modules are often called in the current module. ThinkPHP has built-in two special uppercase letter methods, method A and method R, to handle cross-module calls.
Currently, the Index operation User module in the index module has the showName operation User module and the specific code for the showName operation is as follows:
<?phpclass UserAction extends Action{ public function showName(){ echo "Hello World"; }}?>
We will call the showName operation above in the Index operation of the index module.
Cross-module calling through method
Method A is used to instantiate other modules (when the new keyword is used). After the module is instantiated, operations in the module can be called as objects.
In the Index operation of the index module, call the showName operation instance of the User module:
<? Phpclass IndexAction extends Action {public function index () {header ("Content-Type: text/html; charset = UTF-8 "); // instantiate the User Module $ User = A ('user'); // call the method $ User-> showName () ;}}?> in the User Module
Method A also supports cross-group calling and cross-project calling modules. The syntax is as follows:
A ('<Project name: //> <group name/> module name ')
// Common example A ('user') // call the User module of the current project. For example A ('admin: // user ') // call the User module A ('admin/user') of the Admin project // call the User module A ('admin: // Tool/user') of the Admin group ') // call the User module of the Tool Group of the Admin Project
Cross-module calling through the R Method
ThinkPHP also provides the R method, which can directly call the operation methods of other modules., Change the example using method A to method R:
<? Phpclass IndexAction extends Action {public function index () {header ("Content-Type: text/html; charset = UTF-8 "); // call method R ('user/showname') in the User module; }}?>
The R method also supports cross-group calling and cross-project calling. The syntax is as follows:
R ('<Project name: //> <group name/> Module name/operation' <, array ()>)
// Common example R ('user/showname') // call the showName method of the User module of the current project. For example R ('admin: // User/showname ') // call the showName method R ('admin/User/showname') of the User module of the Admin project // call the showName method R ('admin: // Tool/User/info') // call the info method of the User module of the Tool Group of the Admin project. The R method also supports passing parameters to the called method, in fact, the transfer operation may require parameter input. The second parameter of the R method is an array, which is passed as the parameter of the called operation. Example: R ('user/showname', array (5 ));
In this example, the showName operation will accept the parameter 5. The corresponding showName operation may be:
<? Phpclass UserAction extends Action {public function showName ($ id) {// code used to obtain user information based on the id parameter }}?>
To input multiple parameters, define multiple elements in the array () parameter array of the R method in sequence.
Method A or method R?
From the example above, we can see that both method A and method R can call the operations of other modules. Is it better to use method A or method R? The suggestions are as follows:If you want to use multiple methods in other modules, we recommend that you use method.To call different methods of this module through objects to avoid multiple Object Instantiation;If you only need to use one of the methods in other modules, the R method is undoubtedly the most concise.