2011-09-22 66 views
0

我有一个扩展mysqli_result类的问题。PHP扩展mysqli_result

我想用自定义类扩展mysqli_result类。 这里是我的代码:

class mysqli_result_extended extends mysqli_result { 

    public function GetJSON() { 
    blah blah... 
    return $json; 
    }  
} 

$db = new mysqli('localhost','root','*****','somedb'); 

$sql = 'SELECT * FROM students'; 

$result = $db->query($sql); 

$result->getJSON(); //This is causing the trouble 

当我运行上面的代码,它给出了一个错误:

Call to undefined method mysqli_result::getJSON() in ****.php on line ** 

有什么不对的代码?

回答

1

由于$db->query($sql)返回mysqli_result类型的变量而不是mysqli_result_extended,您会收到错误消息。 mysqli_result类没有名为getJSON的方法。

所以,当你定义一个类B扩展A类,它并不意味着基类A的所有实例都奇迹般地成为B类。