欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-06 18:58:25
复制代码 代码如下:

复制代码 代码如下:

<?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 (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   公有的:本类,子类,外部对象都可以调用
protected 受保护的:本类 子类,可以执行,外部对象不可以调用
private 私有的:只能本类执行,子类与外部对象都不可调用