← Back to PHP Course | Chapter 5: Functions | Lesson 10 of 10

PHP Callback Functions

एक callback किसी दोस्त को अपना phone number देकर कहने जैसा है 'जब यह मिल जाए तो मुझे call करना'। आप एक function को दूसरे function को सौंपते हैं ताकि यह सही moment पर आपका function चला सके।
Syntax
php
$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
<?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);
?>

एक 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
<?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));
?>

एक 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
<?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);
?>

एक 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
<?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);
?>

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
<?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);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. quotes के बिना एक function name pass करना, जैसे array_map(strtoupper, $a), जिसे PHP एक undefined constant मानकर पढ़ता है।
  2. एक private method के साथ [$obj, method] लिखना, जो fail हो जाता है क्योंकि callable इसे बाहर से access नहीं कर सकता।
  3. यह भूल जाना कि array_map callbacks सिर्फ values receive करते हैं, जबकि ARRAY_FILTER_USE_KEY वाला array_filter keys receive करता है, इसलिए functions के बीच arguments अलग होते हैं।
चैप्टर सारांश
  • Functions parameters और default arguments लेते हैं, और values return करते हैं।
  • Variable functions, anonymous functions, arrow functions, और callbacks functions को values की तरह treat करते हैं।
  • Recursion और scope decide करते हैं कि functions कैसे दोहराते हैं और कौन से variables देखते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.