Php learning notes-Object-oriented programming _ PHP Tutorial

Source: Internet
Author: User
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...

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.