【发布时间】:2017-03-16 08:30:22
【问题描述】:
我是php新手!我的任务是创建具有构造函数的类(Request),该构造函数具有一个参数($_SERVER)和另一个扩展Request的类(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 的输出如下:
获取
http://
{"":null}
预期输出是:
获取
获取
/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
{"a":"1","b":"2"}
我看不出哪里有问题!看起来 $server 没有初始化,但我现在不知道为什么!
【问题讨论】:
-
您在
__contruct中缺少一个's' a,它应该是__construct
标签: php