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

PHP Variable Functions

एक variable function किसी note में एक phone number रखने और वह जो भी कहे उसे dial करने जैसा है। PHP variable में लिखा text पढ़ता है और उस नाम वाले function को call करता है।
Syntax
php
$function_name = "existing_function";
$function_name($argument);            // calls existing_function()
$object->$method_name($argument);     // variable method
ClassName::$method_name();            // variable static method

Variable Functions का Introduction

अगर कोई variable एक string रखता है और आप इसमें () append करते हैं — $fn() — PHP उस function को ढूँढता है जिसका *name* string की current value से match करता है और उसे call करता है।

यह indirection ही है जो आपको runtime पर, hardcoded logic के बजाय data के आधार पर, कई functions में से कौन सा invoke करना है यह decide करने देता है।

उदाहरण: Introduction to Variable Functions

php
<?php
// Define the function `sayHello` with no parameters
function sayHello() {
    // Print "Hello!" to the output
    echo "Hello!";
}
// Declare `$fn`, set to "sayHello"
$fn = "sayHello";
$fn();
?>

Variable Object Methods

वही mechanism object methods तक भी extend होता है: एक method name को एक variable में string की तरह store करना और $object->$methodName() call करना उस method को dynamically invoke करता है, calling code को यह hardcode किए बिना कि कौन सा specific method चलाना है।

उदाहरण: Variable Object Methods

php
<?php
// Define the class `Greeter`
class Greeter {
    // Define the function `hello` with no parameters
    function hello() {
        // Print "Hi there!" to the output
        echo "Hi there!";
    }
}
// Create a new `Greeter` instance, stored in `$obj`
$obj = new Greeter();
// Declare `$methodName`, set to "hello"
$methodName = "hello";
$obj->$methodName();
?>

Variable Static Methods

आप किसी class के static method को इसी तरह dynamically call कर सकते हैं, class name, method name, या दोनों को strings की तरह रखकर और उन्हें call time पर combine करके — यह dispatch tables के लिए उपयोगी है जो runtime data के आधार पर अलग handlers की ओर route करते हैं।

उदाहरण: Variable Static Methods

php
<?php
// Define the class `Math`
class Math {
    // Define the function `square` taking `$n`
    static function square($n) {
        // Return `$n * $n` from this function
        return $n * $n;
    }
}
// Declare `$class`, set to "Math"
$class = "Math";
// Declare `$method`, set to "square"
$method = "square";
// Print `$class::$method(4)` to the output
echo $class::$method(4);
?>

Call करने से पहले Check करना

इस तरह कुछ भी call करने से पहले, पहले is_callable($value) check करें।

एक ऐसी string invoke करने की कोशिश करना जो actually किसी real function या method से corresponding नहीं है gracefully fail होने के बजाय एक fatal error throw करता है, इसलिए पहले validate करना एक bad या mistyped name से एक crash को रोकता है।

उदाहरण: Checking Before Calling

php
<?php
// Declare `$fn`, set to "strtoupper"
$fn = "strtoupper";
// Check whether `is_callable($fn)`
if (is_callable($fn)) {
    // Print `$fn("hello")` to the output
    echo $fn("hello");
}
?>

Callbacks में Variable Functions

किसी string function name को array_map() या usort() जैसी किसी चीज़ को callback की तरह pass करना under the hood exactly इसी variable-function mechanism पर depend करता है — PHP हर element के लिए call करने के लिए string को एक actual function में resolve कर देता है, बिना आपके खुद एक explicit dispatch step लिखे।

उदाहरण: Variable Functions in Callbacks

php
<?php
// Define the function `double` taking `$n`
function double($n) {
    // Return `$n * 2` from this function
    return $n * 2;
}
// Declare `$numbers` as an array: `[1, 2, 3]`
$numbers = [1, 2, 3];
// Declare `$doubled`, set to `array_map("double", $numbers)`
$doubled = array_map("double", $numbers);
// Print a human-readable dump of `$doubled`
print_r($doubled);
?>
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. एक ऐसा variable function call करना जिसका नाम exist नहीं करता, जैसे $fn = nofunc; $fn();, जो एक fatal 'Call to undefined function' error का कारण बनता है।
  2. user input से function name बनाना, जो visitors को कोई भी function चलाने देता है; एक allow-list के against check करें।
  3. echo या print जैसे language constructs को एक variable के through call करने की कोशिश करना, जो काम नहीं करता क्योंकि वे real functions नहीं हैं।

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.