Where is my oop wrong? Is the problem of static variables?
Class db{
public $conn;
private static $h = "localhost";
private static $u = "root";
private static $p = "123123";
private static $d = "Air";
function __construct () {
$this->conn=mysqli_connect (self:: $h, Self:: $u, Self:: $p, Self:: $d);
$this->conn->query (' SET NAMES UTF8 ');
Mysqli_query ($this->conn, ' SET NAMES UTF8 ');
}
Public Function GetOne ($sql, $resultType =mysql_assoc) {//fetch only one bar
$q = $this->conn->query ($sql);
$rt = $q->fetch_array ($resultType);
return $rt;
}
}
The following is an inheritance class, that is, the above DB I have written. There is a reference to the database (only one, take a number of I did not write) to inherit. Isn't it good to write? Thank you
Class Airvia extends db{
Public Function Dosql () {
$sql = ' SELECT * from xls1 where id=1 ';//The actual statement is complex, just test
$r = $this->getone ($sql);//db::getone ($sql);
}
}//airvia
Use this class
$WC =new Airvia ($v); Echo $WC->dosql ();
The error message is as follows:
Fatal Error:call to a member function query () on a non-object in D:\xampp\htdocs\chaxun\class\db.class.php on line 67
Excuse me:
My idea, right? Thank you
First, write a DB class, to connect to the database, the library to operate the database, you can inherit;
Second, the above statement why the error, it seems that the $this->conn into a static variable also not.
------Solution--------------------
Should be conn object not generated successfully, check the parameters of what, print out the Conn object to see what
------Solution--------------------
Fatal Error:call to a member function query () on a non-object
$this->conn=mysqli_connect (self:: $h, Self:: $u, Self:: $p, Self:: $d);
No instantiation succeeded
But your code is not a problem, you can pass the test (change the user name, password, library name and table name, of course)
The error could have been generated elsewhere, and it was on line 67, how much did you post?
Since it is encapsulated, the error handling should also be encapsulated in. So don't go around asking why it's wrong.
------Solution--------------------
You need to initialize the constructor method of the parent class
------Solution--------------------
Add a constructor to subclass Airvia
Public Function __construct () {
Parent::__construct ();
}
------Solution--------------------
Need to be re-structured.
Reference:
Add a constructor to subclass Airvia
PHP code?123public function __construct () {parent::__construct ();}