解析错误爆炸
Parse error on explode
我收到一个解析错误,不知道为什么
<?php
class api{
//$api_Key;
public function getURL($url){
return file_get_contents($url);
}
public function postURL($url){
$data = array();
$data_string = json_encode($data);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec( $ch );
print curl_error($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
}
class geo extends api{
public function getLocation(){
return $this->getJSON($this->postURL("https://www.googleapis.com/geolocation/v1/geolocate?key="));
}
}
class vote extends api {
private $key = "";
//private $cordinates = $_COOKIE["cord"];
private $cookie = explode(',',$_COOKIE['cord']);
function getElections(){
return $this->getJSON($this->postURL("https://www.googleapis.com/civicinfo/v2/elections?fields=elections&key=$key"));
}
function getLocationInfo(){
$lat = $cookie[0];
$long = $cookie[1];
return $this->getJSON($this->postURL("https://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat",".$long."&key=$key"));
}
function getDivision(){
}
}
?>
这是错误所在的行
private $cookie = explode(',',$_COOKIE['cord']);
这是错误
Parse error: syntax error, unexpected '(', expecting ',' or ';' in G:\wamp\www\voting\php\vote.php on line 30
我查看了文档和此站点周围的内容,语法看起来正确,但仍然无法解决此错误
<?php
include("/php/vote.php");
$vote = new vote;
$vote->getLocationInfo();
//echo $_COOKIE["cord"];
?>
在 class 中,您不能使用函数声明属性。
您可以为此使用 __construct()
:
class vote extends api {
private $key = "";
//private $cordinates = $_COOKIE["cord"];
private $cookie = array();
function __construct(){
$this->cookie = explode(',',$_COOKIE['cord']);
}
(...)
}
请注意:
我根据你自己的例子写了上面的例子,其中父class没有__construct()
。如果real parent class有一个__construct()
方法,就得这样修改代码:
function __construct( $arg1, $arg2, ... ){
parent::__construct( $arg1, $arg2, ... );
$this->cookie = explode(',',$_COOKIE['cord']);
}
$arg1
等...是父级的参数 class __construct()
:
我收到一个解析错误,不知道为什么
<?php
class api{
//$api_Key;
public function getURL($url){
return file_get_contents($url);
}
public function postURL($url){
$data = array();
$data_string = json_encode($data);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec( $ch );
print curl_error($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
}
class geo extends api{
public function getLocation(){
return $this->getJSON($this->postURL("https://www.googleapis.com/geolocation/v1/geolocate?key="));
}
}
class vote extends api {
private $key = "";
//private $cordinates = $_COOKIE["cord"];
private $cookie = explode(',',$_COOKIE['cord']);
function getElections(){
return $this->getJSON($this->postURL("https://www.googleapis.com/civicinfo/v2/elections?fields=elections&key=$key"));
}
function getLocationInfo(){
$lat = $cookie[0];
$long = $cookie[1];
return $this->getJSON($this->postURL("https://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat",".$long."&key=$key"));
}
function getDivision(){
}
}
?>
这是错误所在的行
private $cookie = explode(',',$_COOKIE['cord']);
这是错误
Parse error: syntax error, unexpected '(', expecting ',' or ';' in G:\wamp\www\voting\php\vote.php on line 30
我查看了文档和此站点周围的内容,语法看起来正确,但仍然无法解决此错误
<?php
include("/php/vote.php");
$vote = new vote;
$vote->getLocationInfo();
//echo $_COOKIE["cord"];
?>
在 class 中,您不能使用函数声明属性。
您可以为此使用 __construct()
:
class vote extends api {
private $key = "";
//private $cordinates = $_COOKIE["cord"];
private $cookie = array();
function __construct(){
$this->cookie = explode(',',$_COOKIE['cord']);
}
(...)
}
请注意:
我根据你自己的例子写了上面的例子,其中父class没有__construct()
。如果real parent class有一个__construct()
方法,就得这样修改代码:
function __construct( $arg1, $arg2, ... ){
parent::__construct( $arg1, $arg2, ... );
$this->cookie = explode(',',$_COOKIE['cord']);
}
$arg1
等...是父级的参数 class __construct()
: