[英]Syntax error expecting identifier in Opencart
I was just checking functionality of opencart's cart controller. 我只是在检查opencart购物车控制器的功能。 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_''
. 它给了我一个错误
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']}
如果不是,那为什么我必须使用花括号
$this->{'model_total_' . $result['code']}
$this->{'model_total_' . $result['code']}
for it. $this->{'model_total_' . $result['code']}
。 Can anyone explain. 谁能解释。
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. 您甚至可以注意到,我的第一个示例在没有
{}
情况下可以工作,因为它是普通变量。
如果要使用表达式作为属性名称,则必须将其放在{}
除非它只是一个变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.