Home  >  Article  >  Backend Development  >  How PHP uses Callback/Callable types

How PHP uses Callback/Callable types

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-05-28 17:54:442184browse

This article will introduce to you how to use the Callback/Callable type in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How PHP uses Callback/Callable types

PHP Callback/Callable type usage

Test code

<?php

/**
 * Class ParentCallback
 */
class ParentCallback
{
    /**
     * @param  int  $a
     * @param  int  $b
     * @param  int  $c
     *
     * @return int
     */
    public static function parentSum(int $a, int $b, int $c)
    {
        return $a + $b + $c;
    }

    /**
     * @param  int  $a
     * @param  int  $b
     * @param  int  $c
     *
     * @return float|int
     */
    public function parentMultiply(int $a, int $b, int $c)
    {
        return $a * $b * $c;
    }
}

/**
 * Class Callback
 */
class Callback extends ParentCallback
{

    /**
     * @param  int  $a
     * @param  int  $b
     *
     * @return int
     */
    public static function sum(int $a, int $b)
    {
        return $a + $b;
    }

    /**
     * @param  int  $a
     * @param  int  $b
     *
     * @return float|int
     */
    public function multiply(int $a, int $b)
    {
        return $a * $b;
    }
}

/**
 * Class Invoke
 */
class Invoke
{

    /**
     * @param  int  $a
     * @param  int  $b
     *
     * @return float|int
     */
    public function __invoke(int $a, int $b)
    {
        return $a / $b;
    }
}

/**
 * @param  int  $a
 * @param  int  $b
 *
 * @return int
 */
function sum(int $a, int $b)
{
    return $a + $b;
}

/**
 * @param $callback
 * @param  mixed  ...$parameter
 *
 * @return mixed
 */
function do_something(callable $callback, ...$parameter)
{
    return call_user_func($callback, ...$parameter);
}

Test example

// 闭包
$ret = do_something(function ($a, $b){
    return $a - $b;
}, 5, 6);
printf("闭包测试示例: %s\n", $ret);

// 函数
$ret = do_something(&#39;sum&#39;, 5, 6);
printf("函数测试示例: %s\n", $ret);

// 静态方法
$ret = do_something([Callback::class, &#39;sum&#39;], 5, 6);
printf("静态方法示例: %s\n", $ret);

$ret = do_something(&#39;\Callback::sum&#39;, 5, 6);
printf("静态方法示例: %s\n", $ret);

$ret = do_something([Callback::class, &#39;self::parentSum&#39;], 5, 6, 7);
printf("静态方法示例: %s\n", $ret);

$ret = do_something([Callback::class, &#39;parentSum&#39;], 5, 6, 7);
printf("静态方法示例: %s\n", $ret);

$ret = do_something([Callback::class, &#39;parent::parentSum&#39;], 5, 6, 7);
printf("静态方法示例: %s\n", $ret);

// 方法
$callback = new Callback;
$ret = do_something([$callback, &#39;multiply&#39;], 5, 6);
printf("普通方法示例: %s\n", $ret);

// invoke
$invoke = new Invoke;
$ret = do_something($invoke, 5, 6);
printf("对象 invoke 示例: %s\n", $ret);

Test results

闭包测试示例: -1
函数测试示例: 11
静态方法示例: 11
静态方法示例: 11
静态方法示例: 18
静态方法示例: 18
静态方法示例: 18
普通方法示例: 30
对象 invoke 示例: 0.83333333333333

Recommended learning: php video tutorial

The above is the detailed content of How PHP uses Callback/Callable types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete