这是在 PHP 5.3 中给我错误的行,它在 php 5.4 中完美运行
if ($user->getGeoCode()) {
$latitude = $user->getGeoCode()['latitude'];
}
错误消息是:
Parse error: syntax error, unexpected '[' in IndexController.php on line 29
这是我的用户类别:
class User {
.....
public function getGeoCode() {
$geoCode=array();
if ($this->getAddress() && $this->getCity() && $this->getCountry()) {
$address = urlencode($this->getAddress() . ' ' . $this->getCity() . ' ' . $this->getPostalCode() . ' ' . $this->getCountry()->getName());
$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=' . $address . '&sensor=false');
$output = json_decode($geocode);
if ($output->status=='OK') {
$geoCode['latitude']=$output->results[0]->geometry->location->lat;
$geoCode['longitude']=$output->results[0]->geometry->location->lng;
return $geoCode;
}
else {
return null;
}
}
else {
return null;
}
}
}
这个错误与 PHP 版本有关吗?
错误消息说明了一切。 PHP5.3不支持将函数输出自动转换为数组并像这样访问它。
从 PHP 5.4 开始,就可以做到这一点Example http://php.net/manual/en/language.types.array.php#example-88
在 PHP 5.3 之前,您需要使用临时变量。
EDIT
by casting
I ment dereferencing
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)