callback function in php
* 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 '<hr>'; echo call_user_func(function($site){ return '欢迎来到'.$site; },'php中文网'); echo '<hr>';
//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 '欢迎来到'.$site.'学习'.$course.'课程'; },'php中文网','php'); echo '<hr>'; //支持调用类中的方法做为回调 class Demo1 { //普通方法 public function sum($a, $b) { return $a+$b; } } class Demo2 { //静态方法 public static function multi($a,$b) { return $a*$b; } }
//Call a normal method: the callback must be passed in as an array: [object, 'method name']
$obj = new Demo1; echo call_user_func([$obj,'sum'],10,15);
//php5 .4 or above, can be abbreviated:
echo call_user_func([(new Demo1()),'sum'],10,15); echo '<hr>';
//Call static method: there are two ways: string and array
//String: 'Class name::Method name'
echo call_user_func('Demo2::multi',10,15);
//Array: [class name, 'method name']
echo '<hr>'; echo call_user_func(['Demo2','multi'],20,5);
//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 '<hr>'; echo call_user_func_array(function(){ //查看传递给回调的参数 // return print_r(func_get_args(),true); $msg = '前端开发的基础知识包括:'; //遍历参数数组 foreach (func_get_args() as $value) { $msg .= $value.','; } //去掉未尾逗号,并替换成感叹号 return rtrim($msg,',').'!'; }, ['html','css','javascript']); echo '<hr>'; class Demo3 { public function func1() { return __METHOD__.'<pre class="brush:php;toolbar:false">'.print_r(func_get_args(),true).''; } } class Demo4 { public static function func2() { return __METHOD__.'
'.print_r(func_get_args(),true).''; } }
//Methods in callback classes
echo call_user_func_array([(new Demo3),'func1'], ['php','java','c']);
//Callbacks for calling static methods:
//1. Use array parameter method
echo call_user_func_array(['Demo4','func2'], ['html','css','jQuery']);
//2. Use string parameter method
echo call_user_func_array('Demo4::func2', ['html','css','jQuery']);
//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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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. 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

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

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

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.

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.

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.
