2016-03-07 121 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