PHP Variable Functions
In this page:
$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
// Define the function `sayHello` with no parameters
function sayHello() {
// Print "Hello!" to the output
echo "Hello!";
}
// Declare `$fn`, set to "sayHello"
$fn = "sayHello";
$fn();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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");
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- एक ऐसा variable function call करना जिसका नाम exist नहीं करता, जैसे
$fn = nofunc; $fn();, जो एक fatal 'Call to undefined function' error का कारण बनता है। - user input से function name बनाना, जो visitors को कोई भी function चलाने देता है; एक allow-list के against check करें।
echoयाprintजैसे language constructs को एक variable के through call करने की कोशिश करना, जो काम नहीं करता क्योंकि वे real functions नहीं हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: