This time I will bring you PHP callback function and anonymous functionuse case analysis, what are the notes when using PHP callback function and anonymous function, The following is a practical case, let’s take a look.
1. Callback function
#PHP’s callback function actually has the same function as the callback function in C, Java and other languages. During the execution of the main thread, it suddenly jumps to execute the set callback function;
After the callback function is executed, it returns to the main thread to process the next process
And the callback is called in php Functions don’t want to directly use the function name as a function parameter like C and Java. Instead, use the string name corresponding to the function in PHP to execute
1.1, no-parameter callback
<?php //无参数回调 function callback(){ echo 'execute no parameters callback.<br/>'; } function main($callback){ echo 'execute main start.<br>'; $callback(); echo 'execute main end.<br>'; } main('callback'); //结果 ecute main start. execute no parameters callback. execute main end.
1.2, global callback Function
<?php //全局函数回调 function callback($a,$b){ echo "$a<====>$b.<br>"; } $func = 'callback'; call_user_func($func, 1,2); call_user_func_array($func, array(1,2)); //结果 12. 12.
1.3, class method and static method callback
<?php class Test{ //成员函数 function callback($a,$b){ echo "callback $a<====>$b.<br>"; } public static function staticCallback($a,$b){ echo "staticCallback $a$b.<br>"; } } //非静态方法调用方式一 $test = new Test(); call_user_func(array($test, 'callback'), 1,2); call_user_func_array(array($test, 'callback'), array(1,2)); //非静态方法调用方式二 $func = 'callback'; $test->$func(7,9); //静态方法调用方式 call_user_func(array('Test', 'staticCallback'), 4,6); call_user_func_array(array('Test', 'staticCallback'), array(4,6)); call_user_func_array("Test::staticCallback", array(4,6)); //结果 callback 12. callback 12. callback 79. staticCallback 46. staticCallback 46. staticCallback 46.
2, anonymous function
2.1, in php Anonymous functions, also called closures, allow you to specify a function without a name. The most commonly used ones are callback function parametersvalue
<?php $closureFunc = function($str){ echo $str.'<br/>'; }; $closureFunc("hello world!"); //结果 hello world!
2.2, closure
2.2.1, passing in parameters and referencing local variables
<?php $closureFunc = function($name){ $sex = '男'; $func = function($age)use ($name,$sex){ echo "$name--$sex--$age<br/>"; }; $func(23); }; $func = $closureFunc("lvfk"); //结果 lvfk--男--23
2.2.2. Return closure function
<?php $closureFunc = function($name){ echo 'closureFunc '; $sex = '男'; echo "$name+++$sex<br/>"; $func = function()use ($name,$sex){ echo "$name--$sex<br>"; }; return $func; }; $func = $closureFunc("lvfk"); $func(); $func(); //结果 closureFunc lvfk+++男 lvfk--男 lvfk--男
2.2.3. Closure changes the value of context, which requires reference passing
<?php $closureFunc = function($name){ $age = 1; echo "$name+++$age<br/>"; $func = function()use ($name,&$age){ $age++; echo "$name--$age<br>"; }; return $func; }; $func = $closureFunc("lvfk"); $func(); $func(); $func(); //结果 lvfk+++1 lvfk--2 lvfk--3 lvfk--4
The above is a simple application of closure , through closure, we can see that when using closure outside the function, the parameter content passed into the closure can actually be the content of the context object.
The context object value can also be changed within the closure, but it must It is passing by reference
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of the steps to implement one-way hash encryption in PHP
Detailed explanation of the steps to hide the entry file in PHP
The above is the detailed content of PHP callback function and anonymous function use case analysis. For more information, please follow other related articles on the PHP Chinese website!

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

Article discusses creating and securing PHP APIs, detailing steps from endpoint definition to performance optimization using frameworks like Laravel and best security practices.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor
