How PHP uses Callback/Callable types

醉折花枝作酒筹
Release: 2023-03-10 07:00:01
forward
2205 people have browsed it

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);
}
Copy after login

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);
Copy after login

Test results

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

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!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template