一個php初學者的一個學習筆記的面向對象編程實例,有需要學習的朋友可參考參考.
PHP實例代碼如下:
- class db {
- private $mysqli; //數據庫連接
- private $options; //SQL選項
- private $tableName; //表名
- 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 (emptyempty ( $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 公有的:本類,子類,外部對象都可以調用.
protected 受保護的:本類 子類,可以執行,外部對象不可以調用.
private 私有的:只能本類執行,子類與外部對象都不可調用.
新聞熱點
疑難解答