My little brother, where is my oop wrong? Is it a matter of static variables?

Source: Internet
Author: User
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 ();}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.