In PHP, a “variable function” refers to a variable that assigns a function name within parentheses. When calling a variable function, the PHP interpreter checks if it contains a callback or a callable function. If it doesn’t, it will produce an error.
Table Of Contents
Here are a few examples that demonstrate this concept.
How to Assign a Function to Variable using PHP
As previously explained, the concept of a variable function in PHP allows for the use of a string variable to call a callback function. To illustrate, consider the following example.
<?php
$func = "codedtag";
function codedtag() {
echo "Welcome to CodedTag.com Tutorials.";
}
$func(); // Welcome to CodedTag.com Tutorials.
The example assigns a string value to the variable $func
containing the function name “codedtag”. After defining the function, calling the variable $func
with parentheses executes the codedtag() function.
However, calling the $func
variable with parentheses will produce an error if it contains a string with an undefined function name. Here’s an example to demonstrate this scenario.
<?php
$func = "calling_func";
$func();
If a variable function is called with parentheses and contains the name of an undefined function, it will produce an error.
This is because the parentheses indicate that the variable is storing a callback function name as a string. For example, if the variable $calling_func contains a string value of “undefined_function”, calling $calling_func()
will result in an error since “undefined_function” is not a defined function.
Fatal error: Uncaught Error: Call to undefined function calling_func() in /tmp/index.php:4 Stack trace: #0 {main} thrown in /tmp/index.php on line ..
Assign a PHP Function of Class as a Variable
In PHP, you can use the methods of a class as variables with parentheses to call them once an instance of the class is created.
<?php
class team {
public function group() {
echo "Our team is ready to start the challenge";
}
}
$obj = new team();
$group = "group";
$obj->$group();
In this example, we can see that the $group
variable stores the name of the method that can be called by using parentheses.
If the class contains a static property, consider the following example:
<?php
class team {
public static function group() {
echo "Our team is ready to start the challenge";
}
}
$obj = new team();
$group = "group";
$obj::$group();
One way to call the method using the variable name as a method name is by using the scope resolution operator like so: $obj::$group()
.
Another way that works for PHP 5.4 and above is using a complex callable.
<?php
$obj = new team();
$group = array($obj, "group");
$group();
Consider the following example to see how a static function in a class can work:
<?php
class team{
public static function group() {
echo "Static Method.";
}
}
$group = array( "team", "group");
$group();
In both of these examples, the array acts as a function by implementing the class object. With the function as a callable once it finds that the main variable has parentheses.
Assign a Function Callback to Variable with Arguments
You can follow the same approach and pass the arguments into the parentheses of the variable.
<?php
$array = array( "item 1", "item 2", "item 3" );
$func = "count";
echo $func($array); // 3
In the above example, the $func variable contains the predefined string name “count”. When invoked with parentheses, PHP looks for the count()
function, which requires a countable element as an argument. However, some PHP predefined callbacks, such as language constructs. (e.g., require
, empty
, include
, isset
, print
, unset
, and echo
), will not work with variables.
Check the below example.
<?php
$value = "Not Empty";
$is_empty = "empty";
var_dump($is_empty());
Executing this example will result in an error as shown below.
Fatal error: Uncaught Error: Call to undefined function empty()
The same technique can be applied to all predefined PHP functions like strlen, strtoupper, count, etc. However, this method won’t work with language constructs like include
, require
, echo
, isset
, empty
, print
, and unset
, as mentioned earlier.
Wrapping Up
Assigning functions to variables in PHP is a powerful technique for creating dynamic and flexible code. It allows developers to pass functions as arguments, store them in arrays or objects, and create more reusable and maintainable code. Overall, the ability to assign functions to variables is a useful tool to have in any PHP developer’s toolbox.
To lean more visit PHP manul.