I am getting a parse error for this and have no idea why
<?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(){
}
}
?>
This is the line the error is on
private $cookie = explode(',',$_COOKIE['cord']);
This is the error
Parse error: syntax error, unexpected '(', expecting ',' or ';' in G:\wamp\www\voting\php\vote.php on line 30
I have looked at the documentation and around this site and the syntax look right but still cant get past this error
<?php
include("/php/vote.php");
$vote = new vote;
$vote->getLocationInfo();
//echo $_COOKIE["cord"];
?>
Inside a class you can't declare properties with functions.
You can use __construct()
for this:
class vote extends api {
private $key = "";
//private $cordinates = $_COOKIE["cord"];
private $cookie = array();
function __construct(){
$this->cookie = explode(',',$_COOKIE['cord']);
}
(...)
}
Please note:
I write above example based on your own example, in which the parent class has not __construct()
. If the real parent class has a __construct()
method, you have to modify the code in this way:
function __construct( $arg1, $arg2, ... ){
parent::__construct( $arg1, $arg2, ... );
$this->cookie = explode(',',$_COOKIE['cord']);
}
$arg1
etc... are the arguments of the parent class __construct()
: