|
Learn how to call functions using strings in php.
1. call_user_func
- Function a ($ B, $ c ){
- Echo $ B;
- Echo $ c;
- }
- Call_user_func ('a, "111", "222 ");
- Call_user_func ('a, "333", "444 ");
- // Display 111 222 333 444
- ?>
The internal method of calling the class is strange. the method actually uses an array and does not know how developers think about it. of course, the new method is saved, which is also innovative:
- Class {
- Function B ($ c ){
- Echo $ c;
- }
- } // Www.jbxue.com
- Call_user_func (array ("a", "B"), "111 ");
- // Display 111
- ?>
2. call_user_func_array
The call_user_func_array function is similar to call_user_func, but the parameter is passed in another way to make the parameter structure clearer:
- Function a ($ B, $ c ){
- Echo $ B;
- Echo $ c;
- }
- Call_user_func_array ('A', array ("111", "222 "));
- // Display 111 222
- ?>
The call_user_func_array function can also call internal methods of the class.
- Class ClassA {
- Function bc ($ B, $ c ){
- $ Bc = $ B + $ c;
- Echo $ bc;
- }
- } // Www.jbxue.com
- Call_user_func_array (array ('classa ', 'BC'), array ("111", "222 "));
- // Display 333
- ?>
Call_user_func and call_user_func_array functions support reference, which makes them more functional than normal function calls:
- Function a (& $ B ){
- $ B ++;
- }
- $ C = 0;
- Call_user_func ('A', & $ c );
- Echo $ c; // Display 1
- Call_user_func_array ('A', array (& $ c ));
- Echo $ c; // Display 2
|