首页 >后端开发 >php教程 >php学习笔记之面向对象编程_php技巧

php学习笔记之面向对象编程_php技巧

WBOY
WBOY原创
2016-05-17 09:07:321048浏览

复制代码代码如下:

class db {
    私有 $mysqli; // 数据库连接
    private $options; //SQL 选项
    private $tableName; // 表名
    public function __construct($tabName) {
        $this->tableName = $tabName;
        $this->db ();
    }
    私有函数 db() {
        $this->mysqli = new mysqli ( 'localhost', 'root', '', 'hdcms' );
        $this->mysqli->query("SET NAMES GBK");
    }
    公共函数字段($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;
        返回 $this;
    }
    public function select() {
        $sql = "SELECT {$this->options['fields']} FROM {$this->tableName}  {$this->options ['命令']}”;
        return $this->query ( $sql );
    }
    私有函数查询($sql) {
        $result = $this->mysqli
            ->query ( $sql );
        $rows = array ();
        while ( $row = $result->fetch_assoc () ) {
            $rows [] = $row;
        }
        返回 $rows;
    }
    private function close() {
        $this->mysqli
            ->close ();
    }
    function __destruct() {
        $this->close ();
    }
}
$chanel = new db ("hdw_channel");
$chanelInfo = $chanel->fields ('id,cname,cpath')
    ->select ();
回显“
”; <br>print_r ( $chanelInfo );
<p>class a { <br>    protected 函数 aa(){ <br>        echo 222; <br>    } <br>} <br>b 类扩展了 a{ <br>    function bb(){ <br>        $this->aa(); <br>    } <br>} <br>$c = new b(); <br>$c->bb();<br></p>

public   公有的:本类,子类,外部对象都可以调用
protected 受保护的:本类子类,可以执行,外部对象不能调用
private 外部的:只能本类执行,子类与外部对象都不可调用
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn