I want to create a function in PHP and to call it without knowing the function's name.
Is this possible?
Example:
<?php
$functionName = "someName";
$this->$functionName();
function someName(){
echo("Print Message");
}
?>
It looks like you want call_user_func().
$functionName = "someName";
call_user_func($functionName); //calls `someName()`
Can be used like the following where the output is the printed message "Hello, World!":
function someName($val){
return $val;
}
$functionName = "someName";
echo call_user_func($functionName, "Hello, World!");
http://ideone.com/Kw5jp
Note: If you're using newer versions of PHP, you can also use callables as @J.Bruni pointed out:
$functionName = "someName";
$functionName(); //also calls `someName()`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With