Reputation: 177
This question may sound stupid, but I want this to work. I've got this code:
$data['method'] = 'get';
$this->app->$data['method']();
How can I replace $data['method']
with get
but no string. I've tried this, but no luck.
$this->app->{$data['method']}();
Any idea?
Upvotes: 0
Views: 72
Reputation: 6534
The last code you posted should work, if you're using PHP 5.4.0 and higher.
<?php
class A {
public function foo() {
echo "yes!";
}
}
class B {
public function run() {
$this->a = new A;
$data = [];
$data['method'] = "foo";
$this->a->{$data['method']}();
}
}
$b = new B;
$b->run();
// Prints "yes!"
Upvotes: 1
Reputation: 19
This will work:
call_user_func(array($this->app, $data['method']));
good luck.
Upvotes: 1