I am newbie with php! I have task to create class(Request) that have constructor which have one parameter ($_SERVER) and another class (GetRequest
Please use the below code. There was a typo in your code, it is __construct and not __contruct :)
<?php
class Request{
protected $server;
public function __construct($ser){
$this->server = $ser;
}
public function getMethod(){
return $this->server['REQUEST_METHOD'];
}
public function getPath(){
return $this->server["PHP_SELF"];
}
public function getURL(){
return 'http://'.$this->server['HTTP_HOST'].$this->server['REQUEST_URI'];
}
public function getUserAgent(){
return $this->server['HTTP_USER_AGENT'];
}
}
class GetRequest extends Request{
function __construct($ser){
parent::__construct($ser);
}
//Return query string params in JSON format
function getData(){
$keywords = preg_split("/[\s,=,&]+/", $this->server['QUERY_STRING']);
$arr=array();
for($i=0;$i<sizeof($keywords);$i++) {
$i++;
if (!isset($keywords[$i])) {
$keywords[$i] = null;
}
$arr[$keywords[$i]] = $keywords[$i];
}
$obj =(object)$arr;
return json_encode($obj);
}
}
echo $_SERVER['REQUEST_METHOD'].'<br/>';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$getReq = new GetRequest($_SERVER);
echo $getReq->getMethod().'<br/>';
echo $getReq->getPath().'<br/>';
echo $getReq->getURL().'<br/>';
echo $getReq->getUserAgent().'<br/>';
echo $getReq->getData().'<br/>';
}
?>