I have a problem with extending the mysqli_result class.
I am trying to extend the mysqli_result class with a custom class.
Here is my code:
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
When I run the above code, it gives an error:
Call to undefined method mysqli_result::getJSON() in ****.php on line **
What’s wrong with this code?
You’re getting the error because the
$db->query($sql)returns a variable of typemysqli_resultand notmysqli_result_extended. Themysqli_resultclass doesn’t have a method namedgetJSON.So when you define a class
Bthat extends classA, it doesn’t mean that all instances of the base classAmagically become classB.