Php learning notes-object-oriented programming. Copy the code as follows :? Phpclassdb {private $ mysqli; database connection private $ options; SQL option private $ tableName; table name publicfunction _ construct ($ tabName) {$ th
The code is as follows:
Class db {
Private $ mysqli; // database connection
Private $ options; // SQL options
Private $ tableName; // table name
Public function _ construct ($ tabName ){
$ This-> tableName = $ tabName;
$ This-> db ();
}
Private function db (){
$ This-> mysqli = new mysqli ('localhost', 'root', '', 'hdcm ');
$ This-> mysqli-> query ("set names gbk ");
}
Public function fields ($ fildsArr ){
If (empty ($ fildsArr )){
$ This-> options ['fields'] = '';
}
If (is_array ($ fildsArr )){
$ This-> options ['fields'] = implode (',', $ fildsArr );
} Else {
$ This-> options ['fields'] = $ fildsArr;
}
Return $ this;
}
Public function order ($ str ){
$ This-> options ['order'] = "order BY". $ str;
Return $ this;
}
Public function select (){
$ SQL = "SELECT {$ this-> options ['fields']} FROM {$ this-> tableName} {$ this-> options ['order']}";
Return $ this-> query ($ SQL );
}
Private function query ($ SQL ){
$ Result = $ this-> mysqli
-> Query ($ SQL );
$ Rows = array ();
While ($ row = $ result-> fetch_assoc ()){
$ Rows [] = $ row;
}
Return $ rows;
}
Private function close (){
$ This-> mysqli
-> Close ();
}
Function _ destruct (){
$ This-> close ();
}
}
$ Chanel = new db ("hdw_channel ");
$ ChanelInfo = $ chanel-> fields ('Id, cname, cpath ')
-> Select ();
Echo"
";
Print_r ($ chanelInfo );Class {
Protected function aa (){
Echo 222;
}
}
Class B extends {
Function bb (){
$ This-> aa ();
}
}
$ C = new B ();
$ C-> bb ();
Public: This class, subclass, and external objects can all be called.
Protected: This class subclass can be executed. external objects cannot be called.
Private: this class can only be executed, and neither the subclass nor the external object can be called.The http://www.bkjia.com/PHPjc/326415.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326415.htmlTechArticle code is as follows :? Php class db {private $ mysqli; // database connection private $ options; // SQL option private $ tableName; // table name public function _ construct ($ tabName) {$ th...