Anton Harald
Anton Harald

Reputation: 827

Call an instance of a class dynamically

$inst_a = new MyClass();
$inst_b = new MyClass();
$inst_c = new MyClass();
(...)

now i want to call a specific function of an instance, depending on which string is provided by an post var, like this:

$inst_name = $_POST['inst_name'];
$inst_name->myfunct($param);

someone an idea?

Upvotes: 0

Views: 42

Answers (1)

Roberto Arosemena
Roberto Arosemena

Reputation: 1140

you can instantiate a class based on a variable, your example security wise isn't very safe but here's how it's done

$inst_name = 'MyClass';
$inst = new $inst_name();
$inst->myfunct($param);

Upvotes: 1

Related Questions