Younger Brother, Where is my oop error? Is it a static variable?

Source: Internet
Author: User
Where is my oop error? Is it a static variable? Class & nbsp; db {public & nbsp; $ conn; & nbsp; private & nbsp; static & nbsp; $ h & nbsp; = & nbsp; "localhost"; & nbsp; where is my oop error? Is it a static variable?
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) {// get only one
$ Q = $ this-> conn-> query ($ SQL );
$ Rt = $ q-> fetch_array ($ resultType );
Return $ rt;
}
}
// The following is an inheritance class, that is, after I write the db above. If you reference a database (retrieve only one record and retrieve more than one record I did not write), you can inherit it. Isn't it easy to write this? 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); no
Return $ r;
}
} // 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:
Is my idea correct? Thank you.
1. write a database class to connect to the database. it can be inherited when the database needs to operate on the database;
2. Why is the preceding statement an error? it seems that the $ this-> conn cannot be changed to a static variable.
------ Solution --------------------
The conn object is not successfully generated. check the parameters and print the conn object.
------ 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 );
Not instantiated

But your code is fine. you can pass the test normally (of course, you have changed the user name, password, database name, and table name)
The error may occur elsewhere, all of which are on line 67. How much do you post?

Since it is encapsulation, the error handling should also be encapsulated. In this way, do not go around and ask why something went wrong.
------ Solution --------------------
You need to initialize the constructor of the parent class
------ Solution --------------------
Add a constructor to the sub-class airVia.
public function __construct(){
parent::__construct();
}

------ Solution --------------------
You need to reconstruct it.
Reference:
Add a constructor to the sub-class airVia.
PHP code? 123 public 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.