PHP - init parent varialbles (with constructor)

*爱你&永不变心* 提交于 2019-12-02 03:57:19

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/>';
}

?>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!