PHP Custom Functions Daquan

Source: Internet
Author: User

1. Call_user_func and Call_user_func_array

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

Class Demo
{
public static function Action1 ()
{
echo "This is demo::action1.<br>";
}

Public Function Action2 ()
{
echo "This is demo::action2.<br>";
}

Public Function Actionwithargs ($arg 1, $arg 2)
{
Echo ' This was Demo::actionwithargs with ($arg 1 = '. $arg 1. ' and $arg 2 = '. $arg 2. ") .<br>";
}
}

$demo = new Demo ();
Call_user_func (Array ("Demo", "Action1"));
Call_user_func (Array ($demo, "Action2"));
Call_user_func (Array ($demo, "Actionwithargs"), "Hello", "World");
Call_user_func_array (Array ($demo, "Actionwithargs"), Array ("Hello2", "world2"));


The results of the operation are as follows:

This is demo::action1.
This is demo::action2.
This is Demo::actionwithargs with ($arg 1 = Hello and $arg 2 = world).
This is Demo::actionwithargs 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.

<?php
function Demoa ()
{
$numOfArgs = Func_num_args ();
$args = Func_get_args ();
echo "The number of the args in Demoa is". $numOfArgs. "<br>";
for ($i = 0; $i < $numOfArgs; $i + +) {
echo "The {$i}th arg is". Func_get_arg ($i). "<br>";
}
echo "-------------------------------------------<br>";
foreach ($args as $arg) {
echo "$arg <br>";
}
}

Demoa (' Hello ', ' world ', ' 123456 ');

The results of the operation are as follows:

The number of args in Demoa is 3
The 0th arg is Hello
The 1th arg is world
The 2th Arg is 123456
-------------------------------------------
Hello
World
123456

3. Register_shutdown_function

The function prototypes and brief descriptions are as follows:

void Register_shutdown_function (callable $callback [, Mixed $parameter [, mixed $ ...]) calls a function that is changed before the script ends 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.

<?php
function Myshutdown ($arg 1, $arg 2)
{
echo ' $arg 1 = '. $arg 1. ', $arg 2 = '. $arg 2. "<br>";
}

if (function_exists (' Myshutdown ')) {
Print "Myshutdown function exists now.<br>";
}

Register_shutdown_function (' Myshutdown ', ' Hello ', ' world ');
Print "This test is executed.<br>";
Exit ();
echo "This comments cannot is output.<br>";

The results of the operation are as follows:

Myshutdown function exists now.
This test is executed.
$arg 1 = Hello, $arg 2 = World

PHP Custom Functions Daquan

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.