PHP - init父变量(带构造函数)

时间:2017-03-16 08:30:23

标签: php

我是php的新手!我有创建类(请求)的任务,该类具有一个参数($ _SERVER)的构造函数和另一个扩展请求的类( GetRequest ) 。我的代码是:

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

?>

http://localhost/HW1/Task3/61807_new.php?http://localhost/HW1/Task3/61807_new.php?a=1&b=2 的输出如下:

  

GET

     

的http://

     

{ “”:空}

预期输出为:

  

获取

     

获取

     

/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,与Gecko一样)Chrome / 56.0.2924.87 Safari / 537.36

     

{ “一个”: “1”, “B”: “2”}

我看不出哪里有问题!看起来 $ server 没有初始化但我现在不知道为什么!

1 个答案:

答案 0 :(得分:2)

请使用以下代码。你的代码中有一个拼写错误,它是__construct而不是__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/>';
}

?>