Home Backend Development PHP Tutorial callback function in php

callback function in php

Jun 28, 2018 pm 02:49 PM

* 2. 2 functions that execute callbacks

* Tip: What is a callback?

* The functions we call provided by the PHP system are called direct calls, also called: direct calls

* When the PHP system calls a user-defined function, it must be called through a proxy function, which is called an indirect call, also called a callback

* This is what we learned today about call_user_func() and call_user_func_array() The proxy function

* can call our custom function instead of the system

* 1.call_user_func_array($callback, $array): Use the array parameters to execute the callback function

* 2.call_user_func($callback, $array): The first parameter is used as the callback function call

* Note: 1. Usually there is no need to write parameters in the callback, but to obtain them through other functions; 2. Parameters Passing by reference is not allowed

//1. Parameters are passed in one by one using: call_user_func()

//Single parameter

echo call_user_func(function(){
    //如果匿名回调没写参数,可以通过func_get_args()获取
    return '欢迎来到'.func_get_arg(0);
},'php中文网');
echo &#39;<hr>&#39;;
echo call_user_func(function($site){
    return &#39;欢迎来到&#39;.$site;
},&#39;php中文网&#39;);
echo &#39;<hr>&#39;;
Copy after login

//Multiple parameters, actual parameter amount The number of callback parameters must be consistent, and any excess will be ignored

echo call_user_func(function($site,$course){
    return &#39;欢迎来到&#39;.$site.&#39;学习&#39;.$course.&#39;课程&#39;;
},&#39;php中文网&#39;,&#39;php&#39;);
echo &#39;<hr>&#39;;
//支持调用类中的方法做为回调
class Demo1
{
    //普通方法
    public function sum($a, $b)
    {
        return $a+$b;
    }
}
class Demo2
{
    //静态方法
    public static function multi($a,$b)
    {
        return $a*$b;
    }
}
Copy after login

//Call a normal method: the callback must be passed in as an array: [object, 'method name']

$obj = new Demo1;
echo call_user_func([$obj,&#39;sum&#39;],10,15);
Copy after login

//php5 .4 or above, can be abbreviated:

echo call_user_func([(new Demo1()),&#39;sum&#39;],10,15);
echo &#39;<hr>&#39;;
Copy after login

//Call static method: there are two ways: string and array

//String: 'Class name::Method name'

echo call_user_func(&#39;Demo2::multi&#39;,10,15);
Copy after login

//Array: [class name, 'method name']

echo &#39;<hr>&#39;;
echo call_user_func([&#39;Demo2&#39;,&#39;multi&#39;],20,5);
Copy after login

//If there is a namespace, just add the namespace string before the class name, or use the constant __NAMESPACE__

//Understand call_user_func(), then call_user_func_array() is very simple, only the parameters are different

* 2.call_user_func_array(callback, array)

* Only accept Two parameters: callback, index array, the parameters passed to the callback are all packed into the index array

* The only difference from call_user_func(array,arg1...) is that the method of passing parameters is different

* Callbacks also support: string function names, class names, arrays, anonymous functions

echo &#39;<hr>&#39;;
echo call_user_func_array(function(){
    //查看传递给回调的参数
//    return print_r(func_get_args(),true);
    
    $msg = &#39;前端开发的基础知识包括:&#39;;
    //遍历参数数组
    foreach (func_get_args() as $value) {
        $msg .= $value.&#39;,&#39;;
    }
    //去掉未尾逗号,并替换成感叹号
    return rtrim($msg,&#39;,&#39;).&#39;!&#39;;
    
}, [&#39;html&#39;,&#39;css&#39;,&#39;javascript&#39;]);
echo &#39;<hr>&#39;;
class Demo3
{
    public function func1()
    {
        return __METHOD__.&#39;<pre class="brush:php;toolbar:false">&#39;.print_r(func_get_args(),true).&#39;
'; } } class Demo4 { public static function func2() { return __METHOD__.'
&#39;.print_r(func_get_args(),true).&#39;
'; } }
Copy after login

//Methods in callback classes

echo call_user_func_array([(new Demo3),&#39;func1&#39;], [&#39;php&#39;,&#39;java&#39;,&#39;c&#39;]);
Copy after login

//Callbacks for calling static methods:

//1. Use array parameter method

echo call_user_func_array([&#39;Demo4&#39;,&#39;func2&#39;], [&#39;html&#39;,&#39;css&#39;,&#39;jQuery&#39;]);
Copy after login

//2. Use string parameter method

echo call_user_func_array(&#39;Demo4::func2&#39;, [&#39;html&#39;,&#39;css&#39;,&#39;jQuery&#39;]);
Copy after login

//If there is a namespace, you only need to add the namespace before the class name Or __NAMESPACE__ will do

The above is the detailed content of callback function in php. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize the lazy loading effect of images through php functions? How to optimize the lazy loading effect of images through php functions? Oct 05, 2023 pm 12:13 PM

How to optimize the lazy loading effect of images through PHP functions? With the development of the Internet, the number of images in web pages is increasing, which puts pressure on page loading speed. In order to improve user experience and reduce loading time, we can use image lazy loading technology. Lazy loading of images can delay the loading of images. Images are only loaded when the user scrolls to the visible area, which can reduce the loading time of the page and improve the user experience. When writing PHP web pages, we can optimize the lazy loading effect of images by writing some functions. Details below

How to write java callback function How to write java callback function Jan 09, 2024 pm 02:24 PM

The writing methods of java callback function are: 1. Interface callback, define an interface, which contains a callback method, use the interface as a parameter where the callback needs to be triggered, and call the callback method at the appropriate time; 2. Anonymous inner class callback , you can use anonymous inner classes to implement callback functions to avoid creating additional implementation classes; 3. Lambda expression callbacks. In Java 8 and above, you can use Lambda expressions to simplify the writing of callback functions.

How to reduce memory usage through php functions? How to reduce memory usage through php functions? Oct 05, 2023 pm 01:45 PM

How to reduce memory usage through PHP functions. In development, memory usage is a very important consideration. If a large amount of memory is used in a program, it may cause slowdowns or even program crashes. Therefore, reasonably managing and reducing memory usage is an issue that every PHP developer should pay attention to. This article will introduce some methods to reduce memory usage through PHP functions, and provide specific code examples for readers' reference. Use the unset() function to release variables in PHP. When a variable is no longer needed, use

Basic syntax and application of callback functions in Java Basic syntax and application of callback functions in Java Jan 30, 2024 am 08:12 AM

Introduction to the basic writing and usage of Java callback functions: In Java programming, the callback function is a common programming pattern. Through the callback function, a method can be passed as a parameter to another method, thereby achieving indirect call of the method. The use of callback functions is very common in scenarios such as event-driven, asynchronous programming and interface implementation. This article will introduce the basic writing and usage of Java callback functions, and provide specific code examples. 1. Definition of callback function A callback function is a special function that can be used as a parameter

Summary of methods for implementing image editing and processing functions using PHP image processing functions Summary of methods for implementing image editing and processing functions using PHP image processing functions Nov 20, 2023 pm 12:31 PM

PHP image processing functions are a set of functions specifically used to process and edit images. They provide developers with rich image processing functions. Through these functions, developers can implement operations such as cropping, scaling, rotating, and adding watermarks to images to meet different image processing needs. First, I will introduce how to use PHP image processing functions to achieve image cropping function. PHP provides the imagecrop() function, which can be used to crop images. By passing the coordinates and size of the cropping area, we can crop the image

Introduction to PHP functions: strtr() function Introduction to PHP functions: strtr() function Nov 03, 2023 pm 12:15 PM

PHP function introduction: strtr() function In PHP programming, the strtr() function is a very useful string replacement function. It is used to replace specified characters or strings in a string with other characters or strings. This article will introduce the usage of strtr() function and give some specific code examples. The basic syntax of the strtr() function is as follows: strtr(string$str, array$replace) where $str is the original word to be replaced.

Comparing PHP functions to functions in other languages Comparing PHP functions to functions in other languages Apr 10, 2024 am 10:03 AM

PHP functions have similarities with functions in other languages, but also have some unique features. Syntactically, PHP functions are declared with function, JavaScript is declared with function, and Python is declared with def. In terms of parameters and return values, PHP functions accept parameters and return a value. JavaScript and Python also have similar functions, but the syntax is different. In terms of scope, functions in PHP, JavaScript and Python all have global or local scope. Global functions can be accessed from anywhere, and local functions can only be accessed within their declaration scope.

How performant are PHP functions? How performant are PHP functions? Apr 18, 2024 pm 06:45 PM

The performance of different PHP functions is crucial to application efficiency. Functions with better performance include echo and print, while functions such as str_replace, array_merge, and file_get_contents have slower performance. For example, the str_replace function is used to replace strings and has moderate performance, while the sprintf function is used to format strings. Performance analysis shows that it only takes 0.05 milliseconds to execute one example, proving that the function performs well. Therefore, using functions wisely can lead to faster and more efficient applications.

See all articles