2016-03-07 115 views
-1

我有這段代碼,我正在擴展一個類,但它說這個方法是未定義的。擴展類中未定義的方法

<?php 
class api{ 
    //$api_Key; 
    public function getURL($url){ 
     return file_get_contents($url); 
    } 
    public function postURL($url){ 
     $ch = curl_init($url); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     $response = curl_exec($ch); 
     curl_close($ch); 
     return $response; 
    } 
    public function getJSON($json){ 
     return json_decode($json); 
    } 
} 
class geo extends api{ 
    public function getLocation(){ 
     return json_decode(postURL("https://www.googleapis.com/geolocation/v1/geolocate?key=$key")); 
    } 
} 
class vote extends geo { 
// Class properties and methods go here 
} 

?> 

PHP文件,查看網頁

<?php 
     include("php/vote.php"); 
      $geo = new geo(); 
      echo $geo->getLocation(); 
     ?> 

我收到此錯誤致命錯誤:

Call to undefined method geo::json_decode() in G:\wamp\www\voting\php\vote.php on line 22

我還沒有找到的東西,會幫我解決這個我有父母和孩子的課程正確或我認爲我是。 它擴展了這個類,所以我不確定它爲什麼沒有定義,任何幫助都很棒。

回答

2

json_decode()是一個內置的PHP函數,而不是任何一個類的方法。您還忘記了您的方法postURL()$this參考。從json_decode()刪除$this和使用$this->postURL

return json_decode($this->postURL("https://www.googleapis.com/geolocation/v1/geolocate?key=$key")); 

但我以爲你真的想用你的方法getJSON()

return $this->getJSON($this->postURL("https://www.googleapis.com/geolocation/v1/geolocate?key=$key")); 
+0

我仍然得到這個錯誤致命錯誤:調用G中未定義功能postURL() :\ wamp \ www \ voting \ php \ vote.php 22行 – Zach

+0

編輯'$ this-> postURL' – AbraCadaver