How to Call a Function from a String Stored in a Variable

How to call a function from a string stored in a variable?

$functionName() or call_user_func($functionName)

Call a function from a stored string in Python

You can do this :

eval(input("What function do you want to call? ") + '()')

How to call PHP function from string stored in a Variable with arguments

Wow one doesn't expect such a question from a user with 4 golds. Your code already works

<?php

function foo ($argument)
{
echo $argument;
}

function bar ($argument)
{
//code here
}

$functionName = "foo";
$argument="Joke";
$functionName($argument); // works already, might as well have tried :)

?>

Output

Joke

Fiddle

Now on to a bit of theory, such functions are called Variable Functions

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

calling the function, which is stored in a string variable

In order to call a function you need to specify the type this function is declared on. If all functions you are going to call are declared on a common class you could do the following:

static void CallFunc(string mymethod)
{
// Get a type from the string
Type type = typeof(TypeThatContainsCommonFunctions);

// Create an instance of that type
object obj = Activator.CreateInstance(type);

// Retrieve the method you are looking for
MethodInfo methodInfo = type.GetMethod(mymethod);

// Invoke the method on the instance we created above
methodInfo.Invoke(obj, null);
}

If the functions you are going to call are static you don't need an instance of the type:

static void CallFunc(string mymethod)
{
// Get a type from the string
Type type = typeof(TypeThatContainsCommonFunctions);

// Retrieve the method you are looking for
MethodInfo methodInfo = type.GetMethod(mymethod);

// Invoke the method on the type
methodInfo.Invoke(null, null);
}

How to call Java function from string stored in a Variable

Yes, you can, using reflection. However, consider also Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection. If at all possible, use interfaces instead. Reflection is rarely truly needed in general application code.

See also

  • Java Tutorials/Reflection API
  • Java Advanced Language Topics/Reflection

Related questions

  • Calling a method named “string” at runtime in Java and C

R - Call a function from function name that is stored in a variable?

You could use get() with an additional pair of ().

a<-function(){1+1}                                                                                                  
var<-"a"

> get(var)()
[1] 2

Calling a function from other python file of whose name is stored in a string variable (dictionary)

Use getattr() instead.

others = {0 : 'num_0', 1 : 'num_1', 2 : 'num_2', 3 : 'num_3', 4 : 'num_4', 5 : 'num_5', 6 : 'num_6', 7 : 'num_7', 8 : 'num_8', 9 : 'num_9'}

#custom_string = 'a0b1c2' #Example

for i in custom_string:
if i.isnumeric():
getattr(letterMatrix, others[i])(self.font_size, self.character, row)

How to call Java method that stored in string variable

You can use the Script Engine with a scripting language, such as JavaScript, to handle your expressions.

package expression;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class App
{
public static void main( String[] args )
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
try {
engine.eval("var test = Packages.expression.App.test");
Object result = engine.eval("test(1,2)");
System.out.println(result);
} catch (ScriptException e) {
//TODO: handle exception
e.printStackTrace();
}
}

public static int test(int x, Double y) {
return x;
}
}

The line "var test = Packages.expression.App.test" creates an alias to your method.

The line Object result = engine.eval("test(1,2)"); calls your method.

You need to include the following dependency to your maven pom.xml:

<dependency>
<groupId>org.openjdk.nashorn</groupId>
<artifactId>nashorn-core</artifactId>
<version>15.1</version>
</dependency>


Related Topics



Leave a reply



Submit