Difference between revisions of "PHP/function variable"
(Created page with "==About== A variable can store a reference to a function. The thing that isn't explained very well in the documentation is that this just consists of naming the function in a...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
==About== | ==About== | ||
− | A variable can store a reference to a function. The thing that isn't explained very well in the documentation is that | + | A variable can store a reference to a function, which can then be called from the variable. |
+ | ===Named Functions/Methods=== | ||
+ | The thing that isn't explained very well in the documentation is that if you want to assign a ''predefined'' function to a variable, you just use a string which is a callable/accessible form of the function. | ||
<syntaxhighlight lang=php> | <syntaxhighlight lang=php> | ||
function Example1() { echo "Example1 was called!\n"; } | function Example1() { echo "Example1 was called!\n"; } | ||
Line 19: | Line 21: | ||
$fEx2(); // prints "Test1 was called!" | $fEx2(); // prints "Test1 was called!" | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | The variable can be used to call the function in exactly the same way as if it had been assigned an anonymous function. | ||
+ | ===Anonymous Functions=== | ||
+ | <small>''[https://www.php.net/manual/en/functions.anonymous.php official documentation]''</small> | ||
+ | <syntaxhighlight lang=php> | ||
+ | $fEx3 = function() { echo "Anonymous function was called!\n"; }; | ||
+ | $fEx3(); // prints "Anonymous function was called!" | ||
+ | </syntaxhighlight> | ||
+ | Unlike with predefined functions, however, the type for the variable is '''Callable''' rather than '''string'''. | ||
+ | ===Functions as Function Parameters=== | ||
+ | If you declare a function parameter as '''Callable''', that parameter will still accept as '''string''' argument, and that argument will remain as a '''string''' (i.e. will not be converted to Callable). | ||
+ | |||
+ | The following code, run after the above examples, illustrates this: | ||
+ | <syntaxhighlight lang=php> | ||
+ | function TestFunc2(Callable $f) { | ||
+ | echo 'Calling the function: '; | ||
+ | $f(); | ||
+ | echo 'Type for f: '.gettype($f)."\n"; | ||
+ | } | ||
+ | TestFunc2($fEx3); | ||
+ | TestFunc2($fEx2); | ||
+ | </syntaxhighlight> | ||
+ | Output: | ||
+ | <blockquote><poem> | ||
+ | Example1 was called! | ||
+ | Test1 was called! | ||
+ | Anonymous function was called! | ||
+ | Calling the function: Anonymous function was called! | ||
+ | Type for f: object | ||
+ | Calling the function: Test1 was called! | ||
+ | Type for f: string | ||
+ | </poem></blockquote> | ||
==Documentation== | ==Documentation== | ||
* [https://www.php.net/manual/en/langref.php Language Reference]: | * [https://www.php.net/manual/en/langref.php Language Reference]: |
Latest revision as of 18:27, 10 April 2022
About
A variable can store a reference to a function, which can then be called from the variable.
Named Functions/Methods
The thing that isn't explained very well in the documentation is that if you want to assign a predefined function to a variable, you just use a string which is a callable/accessible form of the function.
function Example1() { echo "Example1 was called!\n"; }
#$fEx1 = Example1; // does not work; sees "Example1" as a constant
$fEx1 = 'Example1';
$fEx1(); // prints "Example1 was called!"
class cExample2 {
static public function Test1() { echo "Test1 was called!\n"; }
}
#$fEx2 = cExample2::Test1; // does not work; sees "Test1" as a class constant
$fEx2 = 'cExample2::Test1';
$fEx2(); // prints "Test1 was called!"
The variable can be used to call the function in exactly the same way as if it had been assigned an anonymous function.
Anonymous Functions
$fEx3 = function() { echo "Anonymous function was called!\n"; };
$fEx3(); // prints "Anonymous function was called!"
Unlike with predefined functions, however, the type for the variable is Callable rather than string.
Functions as Function Parameters
If you declare a function parameter as Callable, that parameter will still accept as string argument, and that argument will remain as a string (i.e. will not be converted to Callable).
The following code, run after the above examples, illustrates this:
function TestFunc2(Callable $f) {
echo 'Calling the function: ';
$f();
echo 'Type for f: '.gettype($f)."\n";
}
TestFunc2($fEx3);
TestFunc2($fEx2);
Output:
Example1 was called!
Test1 was called!
Anonymous function was called!
Calling the function: Anonymous function was called!
Type for f: object
Calling the function: Test1 was called!
Type for f: string