我是php的新手!我有任務創建類(請求)具有構造函數具有一個參數($ _SERVER)和另一個類(GetRequest)誰擴展請求。我的代碼是:PHP - init父變量(帶構造函數)
<?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://
{ 「」:空}
預計產量爲:
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,例如Gecko)鉻/ 56.0.2924.87 Safari瀏覽器/ 537.36
{ 「一個」: 「1」, 「B」: 「2」}
我不能看看問題在哪裏!它看起來像$服務器不初始化,但我現在不是爲什麼!
你缺少一個「s」一個在'__contruct',它應該是''__construct – gmc