I am newbie with php! I have task to create class(Request) that have constructor which have one parameter ($_SERVER) and another class (GetRequest) who extends Request. My code is:
<?php
class Request{
protected $server;
public function __contruct($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 __contruct($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/>';
}
?>
But the output with http://localhost/HW1/Task3/61807_new.php?http://localhost/HW1/Task3/61807_new.php?a=1&b=2 is following:
GET
http://
{"":null}
Expected output is:
GET
GET
/HW1/Task3/61807_new.php
http://localhost/HW1/Task3/61807_new.php?a=1&b=2
Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
{"a":"1","b":"2"}
I can't see where is problem! It looks like $server is not initialize but i don't now why!
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/>';
}
?>
来源:https://stackoverflow.com/questions/42828818/php-init-parent-varialbles-with-constructor