_php techniques for object-oriented Programming of PHP Learning notes

Source: Internet
Author: User

Copy Code code as follows:

<?php
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 ', ' ', ' hdcms ');
$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 "<pre>";
Print_r ($chanelInfo);

Class A {
protected function AA () {
Echo 222;
}
}
Class B extends a{
function bb () {
$this->aa ();
}
}
$c = new B ();
$c->BB ();


Public publicly owned: This class, subclasses, and external objects can all be called
Protected protected: This class subclass, which can be executed, cannot be invoked by external objects
Private: Only this class can be executed, subclasses and external objects are not callable

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.