Examples of PHP function handling functions

Source: Internet
Author: User

1. Call_user_func and Call_user_func_array:

The above two functions call the callback function in a different parameter form. See the example below:

<?phpclass anothertestclass {public    static function PrintMe () {        print ' This is Test2::p rintself.\n ';    } Public    function dosomething () {        print ' This is Test2::d osomething.\n ";    }    Public Function Dosomethingwithargs ($arg 1, $arg 2) {        print ' Test2::d Osomethingwithargs with ($arg 1 = '. $arg 1. ') and $arg 2 = '. $arg 2. "). \ n ";    }} $TESTOBJ = new Anothertestclass (); Call_user_func (Array ("Anothertestclass", "printMe"); Call_user_func (Array ($ Testobj, "dosomething")); Call_user_func (Array ($TESTOBJ, "Dosomethingwithargs"), "Hello", "World"); call_user_func_ Array (Array ($TESTOBJ, "Dosomethingwithargs"), Array ("Hello2", "world2"));

The results of the operation are as follows:

bogon:testphp$ php call_user_func_test.php This is Test2::p rintself.this are Test2::d osomething.this is Test2:: Dosomethingwithargs with ($arg 1 = Hello and $arg 2 = world). This is Test2::d Osomethingwithargs with ($arg 1 = Hello2 and $arg 2 = world2).

2. Func_get_args, Func_num_args and Func_get_args:

The common feature of these three functions is that they are all custom function parameter related, and can only be used within the function, compared to the custom function for variable parameters. Their function prototypes and brief descriptions are as follows:
int Func_num_args (void) gets the number of arguments for the current function.
Array Func_get_args (void) returns all parameters of the current function as an array.
mixed Func_get_arg (int $arg _num) returns the parameter at the specified position of the current function, where 0 represents the first argument.

<?phpfunction myTest () {    $numOfArgs = Func_num_args ();    $args = Func_get_args ();    Print "The number of args in MyTest is". $numOfArgs. " \ n ";    for ($i = 0; $i < $numOfArgs; $i + +) {        print "the {$i}th arg is". Func_get_arg ($i). " \ n ";    }    print "\ n-------------------------------------------\ n";    foreach ($args as $key = + $arg) {        print "$arg \ n";    }} MyTest (' Hello ', ' world ', ' 123456 ');

The results of the operation are as follows:

stephens-air:testphp$ PHP class_exist_test.php The number of args in MyTest are 3The 0th arg is hellothe 1th arg is worldth E 2th arg is 123456-------------------------------------------helloworld123456

3. Function_exists and Register_shutdown_function:

the function prototypes and brief descriptions are as follows:

bool Function_exists (string $function _name) determines whether a function exists.
void Register_shutdown_function (callable $callback [, Mixed $parameter [, mixed $ ...]) A function that changes registration is called before the end of the script or when exit is called halfway. In addition, if multiple functions are registered, they will be executed sequentially in the order they were registered, and if exit () is called inside of one of the callback functions, the script exits immediately and the remaining callbacks are not executed.

<?phpfunction shutdown ($arg 1, $arg 2) {    print ' $arg 1 = '. $arg 1. ', $arg 2 = '. $arg 2. " \ n ";} if (function_exists (' shutdown ')) {    print "Shutdown function exists now.\n";} Register_shutdown_function (' Shutdown ', ' Hello ', ' world ');p the rint "This test was executed.\n"; exit ();p rint "This comments cannot be output.\n ";

The results of the operation are as follows:

stephens-air:testphp$ php call_user_func_test.php shutdown function exists now. This test was executed. $arg 1 = Hello, $arg 2 = World

Examples of PHP function handling functions

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.