An object-oriented programming example of a learning note for php beginners. if you have any friends who need to learn, you can refer to the example. you can refer to this document for more information.
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.