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
Introduction to Variable Functions
If a variable holds a string and you append () to it — $fn() — PHP looks for a function whose *name* matches that string's current value and calls it.
This indirection is what lets you decide, at runtime, which of several functions to invoke based on data rather than hardcoded logic.
Example: 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
The same mechanism extends to object methods: storing a method name as a string in a variable and calling $object->$methodName() invokes that method dynamically, without the calling code needing to hardcode which specific method to run.
Example: 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
You can dynamically call a class's static method the same way, by holding either the class name, the method name, or both as strings and combining them at call time — useful for dispatch tables that route to different handlers based on runtime data.
Example: 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
Checking Before Calling
Before calling anything this way, check is_callable($value) first.
Attempting to invoke a string that doesn't actually correspond to any real function or method throws a fatal error rather than failing gracefully, so validating first prevents a crash from a bad or mistyped name.
Example: 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
Variable Functions in Callbacks
Passing a string function name to something like array_map() or usort() as a callback relies on exactly this variable-function mechanism under the hood — PHP resolves the string to an actual function to call for each element, without you writing an explicit dispatch step yourself.
Example: 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
- Calling a variable function whose name does not exist, like
$fn = nofunc; $fn();, which causes a fatal 'Call to undefined function' error. - Building the function name from user input, which lets visitors run any function; check against an allow-list.
- Trying to call language constructs like
echoorprintthrough a variable, which does not work because they are not real functions.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: