Reputation: 1619
I was just checking functionality of opencart's cart controller. Where i saw
foreach ($results as $result) {
if ($this->config->get($result['code'] . '_status')) {
$this->load->model('total/' . $result['code']);
$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
}
}
Its working fine. But when i change it to
foreach ($results as $result) {
if ($this->config->get($result['code'] . '_status')) {
$this->load->model('total/' . $result['code']);
$this->'model_total_' . $result['code']->getTotal($total_data, $total, $taxes);
}
}
its giving me an error Parse error: syntax error, unexpected ''model_total_''
. I don't know why this happened. Does it refer to same or not. If not then why i have to use curly braces $this->{'model_total_' . $result['code']}
for it. Can anyone explain.
Upvotes: 0
Views: 122
Reputation: 11987
It is not accepting the concatenation of your result there, so try this,
foreach ($results as $result) {
if ($this->config->get($result['code'] . '_status')) {
$this->load->model('total/' . $result['code']);
$code = 'model_total_' . $result['code'];
$this->$code->getTotal($total_data, $total, $taxes);
}
}
or
foreach ($results as $result) {
if ($this->config->get($result['code'] . '_status')) {
$this->load->model('total/' . $result['code']);
$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes); // wrap with {}
}
}
EDIT
Wrapping with {}
is required when you are using array element for concatenation.
$this->'model_total_' . $result['code']->getTotal($total_data, $total, $taxes);
^ ^
you are using array element for concatenation, So you are getting error.
You can even notice that my first example works without {}
, because its normal variable.
Upvotes: 1
Reputation: 780798
If you want to use an expression as a property name, you have to put it in {}
unless it's just a single variable.
Upvotes: 1