函数处理

call_user_func_array

<?php

/**
 * Calculate the sum of two integers.
 *
 * @param  int    $a
 * @param  int    $b
 * @return int
 */
function foo(int $a, int $b): int
{
    return $a + $b;
}

/**
 * Calculate the sum of two integers.
 *
 * @param  int    $a
 * @param  int    $b
 * @return int
 */
$foo = function (int $a, int $b): int {
    return $a + $b;
};

class Foo
{
    /**
     * Calculate the sum of two integers.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public function method(int $a, int $b): int
    {
        return $a + $b;
    }
}

class Bar
{
    /**
     * Calculate the sum of two integers.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public static function method(int $a, int $b): int
    {
        return $a + $b;
    }
}

class Baz extends Foo
{
    /**
     * Override the parent method.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public function method(int $a, int $b): int
    {
        return $a * $b;
    }
}

class Qux extends Bar
{
    /**
     * Override the parent method.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public static function method(int $a, int $b): int
    {
        return $a * $b;
    }
}

class Quux
{
    /**
     * This method is called when tries to call an object as a function.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public function __invoke(int $a, int $b): int
    {
        return $a + $b;
    }
}

var_dump(call_user_func_array('foo', [1, 2]));                         // int(3)
var_dump(call_user_func_array($foo, [1, 2]));                          // int(3)
var_dump(call_user_func_array([new Foo(), 'method'], [1, 2]));         // int(3)
var_dump(call_user_func_array(['Bar', 'method'], [1, 2]));             // int(3)
var_dump(call_user_func_array('Bar::method', [1, 2]));                 // int(3)
var_dump(call_user_func_array([new Baz(), 'parent::method'], [1, 2])); // int(3)
var_dump(call_user_func_array(['Qux', 'parent::method'], [1, 2]));     // int(3)
var_dump(call_user_func_array(new Quux(), [1, 2]));                    // int(3)

call_user_func

<?php

/**
 * Calculate the sum of two integers.
 *
 * @param  int    $a
 * @param  int    $b
 * @return int
 */
function foo(int $a, int $b): int
{
    return $a + $b;
}

/**
 * Calculate the sum of two integers.
 *
 * @param  int    $a
 * @param  int    $b
 * @return int
 */
$foo = function (int $a, int $b): int {
    return $a + $b;
};

class Foo
{
    /**
     * Calculate the sum of two integers.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public function method(int $a, int $b): int
    {
        return $a + $b;
    }
}

class Bar
{
    /**
     * Calculate the sum of two integers.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public static function method(int $a, int $b): int
    {
        return $a + $b;
    }
}

class Baz extends Foo
{
    /**
     * Override the parent method.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public function method(int $a, int $b): int
    {
        return $a * $b;
    }
}

class Qux extends Bar
{
    /**
     * Override the parent method.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public static function method(int $a, int $b): int
    {
        return $a * $b;
    }
}

class Quux
{
    /**
     * This method is called when tries to call an object as a function.
     *
     * @param  int    $a
     * @param  int    $b
     * @return int
     */
    public function __invoke(int $a, int $b): int
    {
        return $a + $b;
    }
}

var_dump(call_user_func('foo', 1, 2));                         // int(3)
var_dump(call_user_func($foo, 1, 2));                          // int(3)
var_dump(call_user_func([new Foo(), 'method'], 1, 2));         // int(3)
var_dump(call_user_func(['Bar', 'method'], 1, 2));             // int(3)
var_dump(call_user_func('Bar::method', 1, 2));                 // int(3)
var_dump(call_user_func([new Baz(), 'parent::method'], 1, 2)); // int(3)
var_dump(call_user_func(['Qux', 'parent::method'], 1, 2));     // int(3)
var_dump(call_user_func(new Quux(), 1, 2));                    // int(3)

forward_static_call_array

<?php

/**
 * Calculate the sum of two integers.
 *
 * @param  int    $a
 * @param  int    $b
 * @return void
 */
function foo(int $a, int $b)
{
    var_dump($a + $b);
}

class Foo
{
    /**
     * Calculate the sum of two integers.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public function method(int $a, int $b)
    {
        var_dump($a + $b);
    }
}

class Bar
{
    /**
     * Calculate the sum of two integers.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public static function method(int $a, int $b)
    {
        var_dump($a + $b);
    }
}

class Baz extends Foo
{
    /**
     * Override the parent method.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public function method(int $a, int $b)
    {
        var_dump($a * $b);
    }
}

class Qux extends Bar
{
    /**
     * Override the parent method.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public static function method(int $a, int $b)
    {
        var_dump($a * $b);
    }
}

class Quux
{
    /**
     * This method is called when tries to call an object as a function.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public function __invoke(int $a, int $b)
    {
        var_dump($a + $b);
    }
}

class Quuz
{
    /**
     * Call different methods.
     *
     * @param  void
     * @return void
     */
    public static function method()
    {
        forward_static_call_array('foo', [1, 2]);
        forward_static_call_array([new Foo(), 'method'], [1, 2]);
        forward_static_call_array(['Bar', 'method'], [1, 2]);
        forward_static_call_array('Bar::method', [1, 2]);
        forward_static_call_array([new Baz(), 'parent::method'], [1, 2]);
        forward_static_call_array(['Qux', 'parent::method'], [1, 2]);
        forward_static_call_array(new Quux(), [1, 2]);
    }
}

Quuz::method();
// int(3)
// int(3)
// int(3)
// int(3)
// int(3)
// int(3)
// int(3)

forward_static_call

<?php

/**
 * Calculate the sum of two integers.
 *
 * @param  int    $a
 * @param  int    $b
 * @return void
 */
function foo(int $a, int $b)
{
    var_dump($a + $b);
}

class Foo
{
    /**
     * Calculate the sum of two integers.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public function method(int $a, int $b)
    {
        var_dump($a + $b);
    }
}

class Bar
{
    /**
     * Calculate the sum of two integers.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public static function method(int $a, int $b)
    {
        var_dump($a + $b);
    }
}

class Baz extends Foo
{
    /**
     * Override the parent method.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public function method(int $a, int $b)
    {
        var_dump($a * $b);
    }
}

class Qux extends Bar
{
    /**
     * Override the parent method.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public static function method(int $a, int $b)
    {
        var_dump($a * $b);
    }
}

class Quux
{
    /**
     * This method is called when tries to call an object as a function.
     *
     * @param  int    $a
     * @param  int    $b
     * @return void
     */
    public function __invoke(int $a, int $b)
    {
        var_dump($a + $b);
    }
}

class Quuz
{
    /**
     * Call different methods.
     *
     * @param  void
     * @return void
     */
    public static function method()
    {
        forward_static_call('foo', 1, 2);
        forward_static_call([new Foo(), 'method'], 1, 2);
        forward_static_call(['Bar', 'method'], 1, 2);
        forward_static_call('Bar::method', 1, 2);
        forward_static_call([new Baz(), 'parent::method'], 1, 2);
        forward_static_call(['Qux', 'parent::method'], 1, 2);
        forward_static_call(new Quux(), 1, 2);
    }
}

Quuz::method();
// int(3)
// int(3)
// int(3)
// int(3)
// int(3)
// int(3)
// int(3)

func_get_arg

<?php

/**
 * Return all item from the argument list.
 *
 * @param  void
 * @return array
 */
function foo(): array
{
    return [func_get_arg(0), func_get_arg(1), func_get_arg(2)];
}

var_dump(foo(1, 2, 3)); // array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

func_get_args

<?php

/**
 * Return all item from the argument list.
 *
 * @param  void
 * @return array
 */
function foo(): array
{
    return func_get_args();
}

var_dump(foo(1, 2, 3)); // array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }

func_num_args

<?php

/**
 * Return the number of arguments passed to the function.
 *
 * @param  void
 * @return int
 */
function foo(): int
{
    return func_num_args();
}

var_dump(foo(1, 2, 3)); // int(3)

function_exists

<?php

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    //
}

var_dump(function_exists('echo'));  // bool(false)
var_dump(function_exists('print')); // bool(false)
var_dump(function_exists('foo'));   // bool(true)

get_defined_functions

<?php

var_dump(get_defined_functions());

register_shutdown_function

<?php

/**
 * Print a string.
 *
 * @param  void
 * @return void
 */
function foo()
{
    var_dump('foo');
}

register_shutdown_function('foo');
exit();
// string(3) "foo"

register_tick_function

<?php

declare(ticks=1);

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    var_dump('foo');
}

register_tick_function('foo');
$foo = 1;
if ($foo > 0) {
    $foo += 2;
}
// string(3) "foo"
// string(3) "foo"
// string(3) "foo"

unregister_tick_function

<?php

declare(ticks=1);

/**
 * Just a test function.
 *
 * @param  void
 * @return void
 */
function foo()
{
    var_dump('foo');
}

register_tick_function('foo');
unregister_tick_function('foo');
$foo = 1;
if ($foo > 0) {
    $foo += 2;
}
// string(3) "foo"

results matching ""

    No results matching ""