Home > Backend Development > PHP Tutorial > Introduction to the implementation method of php callback function (code)

Introduction to the implementation method of php callback function (code)

不言
Release: 2023-04-04 10:50:01
forward
3281 people have browsed it

本篇文章给大家带来的内容是关于php回调函数的实现方法介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

名称 id 说明 选项options
回调过滤器(callback) 1024 调用自定义函数来过滤数据 callable函数或方法

回调函数实现

回调函数必须接受一个待过滤的值,并返回过滤后的值,回调函数有四种实现方式。

第一种是直接定义回调函数,并在使用过滤器函数时,将回调过滤器的options设置为自定义的回调函数。

<?php
function trimString($value){
    return trim($value);
}
$args=array(
    &#39;options&#39;=>&#39;trimString&#39;,
);
var_dump(filter_var(&#39;abc &#39;,FILTER_CALLBACK,$args));
?>
Copy after login

第二种是在使用过滤器函数时,将回调过滤器的options直接设置为回调函数。

<?php
$args=array(
    &#39;options&#39;=>function ($value){return trim($value);},
);
var_dump(filter_var(&#39;abc &#39;,FILTER_CALLBACK,$args));
?>
Copy after login

第三种是通过闭包实现调用回调函数来传递其他参数。

<?php
function trimString($array){
    return function($value = null) use ($array){
        if(key_exists(&#39;character_mask&#39;,$array)){
            return trim($value,$array[&#39;character_mask&#39;]);  
        }
        return trim($value);
    };
}
$args=array(
    &#39;options&#39;=>trimString(array("character_mask"=>&#39;a..c &#39;)),
);
var_dump(filter_var(&#39;abcd &#39;,FILTER_CALLBACK,$args));
?>
Copy after login

第四种是使用类中的函数作为回调函数,可以用来将多个过滤器回调函数集中到一起。

<?php
class TrimFilter{
    private $options=array();
    private $defaults=array(&#39;character_mask&#39;=>" \t\n\r\0\x0B");
    public function __construct(array $options=array()){
        $this->options = $options;
    }
    
    private function get_options(array $defaults){
        return array_merge($defaults, $this->options);
    }
    
    function trimString($value){
        $ops=$this->get_options($this->defaults);
        if(key_exists(&#39;character_mask&#39;,$ops)){
            return trim($value,$ops[&#39;character_mask&#39;]);  
        }
        return trim($value);
    }
    
    function ltrimString($value){
        $ops=$this->get_options($this->defaults);
        if(key_exists(&#39;character_mask&#39;,$ops)){
            return ltrim($value,$ops[&#39;character_mask&#39;]);  
        }
        return ltrim($value);
    }
    
    function rtrimString($value){
        $ops=$this->get_options($this->defaults);
        if(key_exists(&#39;character_mask&#39;,$ops)){
            return rtrim($value,$ops[&#39;character_mask&#39;]);  
        }
         return rtrim($value);
    }
    
}

$trim_args=array(
    &#39;options&#39;=>array(
        new TrimFilter(array(&#39;character_mask&#39;=>" a")),TRIMSTRING
    )
);
$ltrim_args=array(
    &#39;options&#39;=>array(
        new TrimFilter(array(&#39;character_mask&#39;=>" a")),LTRIMSTRING
    )
);
$rtrim_args=array(
    &#39;options&#39;=>array(
        new TrimFilter(),RTRIMSTRING
    )
);
$str="abcd ";
var_dump(filter_var($str,FILTER_CALLBACK,$trim_args));
var_dump(trim($str," a"));
var_dump(filter_var($str,FILTER_CALLBACK,$ltrim_args));
var_dump(ltrim($str," a"));
var_dump(filter_var($str,FILTER_CALLBACK,$rtrim_args));
var_dump(ltrim($str));
?>
Copy after login


The above is the detailed content of Introduction to the implementation method of php callback function (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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