PHP Callback Functions
In this page:
$result = array_map("function_name", $array); // function name as string
$result = array_map(fn($x) => expression, $array); // closure
$result = array_filter($array, "function_name");
Callback क्या है?
एक callback कोई भी value है जिसे PHP किसी दूसरे function में pass होने पर एक function की तरह call कर सकता है -- एक function का नाम बताती एक string, एक closure, या एक object को एक method name के साथ pair करने वाला एक array।
Callbacks आपको generic array/sorting functions में custom behavior सौंपने देते हैं बिना उन्हें आपकी logic पहले से जाने।
उदाहरण: What Is a Callback?
<?php
// Declare `$numbers` as an array: `[1, 2, 3]`
$numbers = [1, 2, 3];
// Declare `$result`, set to `array_map("strval", $numbers)`
$result = array_map("strval", $numbers);
// Print a human-readable dump of `$result`
print_r($result);
?>
Login to try C/C++/Java/PHP code in the editor
एक Function Name Pass करना
सबसे simple callback बस function का नाम एक string की तरह है, जैसे array_map() को pass किया गया "strtoupper"।
PHP call time पर वह नाम lookup करता है और इसे invoke करता है, जो built-in और user-defined दोनों functions के लिए काम करता है, लेकिन call actually होने तक कोई type-checking offer नहीं करता।
उदाहरण: Passing a Function Name
<?php
// Declare `$words` as an array: `["hello", "world"]`
$words = ["hello", "world"];
// Print a human-readable dump of `array_map("strtoupper", $words)`
print_r(array_map("strtoupper", $words));
?>
Login to try C/C++/Java/PHP code in the editor
एक Closure Pass करना
inline define किया गया एक anonymous function (function($x) { ... }) या arrow function (fn($x) => ...) सबसे common modern callback style है, क्योंकि यह आपको logic को वहीं लिखने देता है जहाँ इस्तेमाल हुआ है बजाय कहीं और एक अलग function को नाम देने के।
Closures use() के through outer variables भी capture कर सकते हैं, जो named functions नहीं कर सकते।
उदाहरण: Passing a Closure
<?php
// Declare `$numbers` as an array: `[1, 2, 3]`
$numbers = [1, 2, 3];
// Declare `$doubled`, set to `array_map(function ($n) { return $n * 2; }, $numbers)`
$doubled = array_map(function ($n) { return $n * 2; }, $numbers);
// Print a human-readable dump of `$doubled`
print_r($doubled);
?>
Login to try C/C++/Java/PHP code in the editor
एक Method Callback Pass करना
किसी object के method को callback की तरह इस्तेमाल करने के लिए, एक two-element array pass करें: एक instance method के लिए [$object, methodName], या एक static के लिए [ClassName, methodName]।
यही तरीका है जिससे आप एक wrapper closure लिखे बिना usort() जैसे functions में object behavior plug करते हैं।
उदाहरण: Passing a Method Callback
<?php
// Define the class `Formatter`
class Formatter {
// Define the function `upper` taking `$s`
function upper($s) { return strtoupper($s); }
}
// Create a new `Formatter` instance, stored in `$formatter`
$formatter = new Formatter();
// Declare `$result`, set to `array_map([$formatter, 'upper'], ["hello", "world"])`
$result = array_map([$formatter, 'upper'], ["hello", "world"]);
// Print a human-readable dump of `$result`
print_r($result);
?>
Login to try C/C++/Java/PHP code in the editor
Common Callback Consumers
array_map() हर element पर एक callback apply करता है और एक नया array return करता है, array_filter() सिर्फ उन elements को रखता है जिनके लिए callback true return करता है, और usort() एक ऐसा callback इस्तेमाल करता है जो एक custom sort order define करने के लिए negative/zero/positive return करता है।
तीनों को exactly वही callback formats ऊपर covered चाहिए।
उदाहरण: Common Callback Consumers
<?php
// Declare `$numbers` as an array: `[1, 2, 3, 4]`
$numbers = [1, 2, 3, 4];
// Print a human-readable dump of `array_map(fn($n) => $n * 2, $numbers)`
print_r(array_map(fn($n) => $n * 2, $numbers));
// Print a human-readable dump of `array_filter($numbers, fn($n) => $n % 2 === 0)`
print_r(array_filter($numbers, fn($n) => $n % 2 === 0));
// Call `usort($numbers, fn($a, $b) => $b <=> $a)`
usort($numbers, fn($a, $b) => $b <=> $a);
// Print a human-readable dump of `$numbers`
print_r($numbers);
?>
Login to try C/C++/Java/PHP code in the editor
- quotes के बिना एक function name pass करना, जैसे
array_map(strtoupper, $a), जिसे PHP एक undefined constant मानकर पढ़ता है। - एक private method के साथ
[$obj, method]लिखना, जो fail हो जाता है क्योंकि callable इसे बाहर से access नहीं कर सकता। - यह भूल जाना कि
array_mapcallbacks सिर्फ values receive करते हैं, जबकिARRAY_FILTER_USE_KEYवालाarray_filterkeys receive करता है, इसलिए functions के बीच arguments अलग होते हैं।
- Functions parameters और default arguments लेते हैं, और values return करते हैं।
- Variable functions, anonymous functions, arrow functions, और callbacks functions को values की तरह treat करते हैं।
- Recursion और scope decide करते हैं कि functions कैसे दोहराते हैं और कौन से variables देखते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: