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

yipeiwu_com6年前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 (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 私有的:只能本类执行,子类与外部对象都不可调用

相关文章

用php来改写404错误页让你的页面更友好

404错误,很多人都知道,如果要访问的url不存在的时候就读取显示这个页面.以往在处理404方面我们通常的做法是要麽简单写几行字,而有心人士或许还会对其稍加美化,另外一少部份想投机取巧的...

php简单防盗链实现方法

本文实例讲述了php简单防盗链实现方法。分享给大家供大家参考。具体如下: <?php $ADMIN = array( 'defaulturl'=> '/zb_...

PHP计算近1年的所有月份

PHP计算近1年的所有月份

话不多说,请看代码:  $z = date('Y-m'); $a = date('Y-m', strtotime('-12 months')); $begin = new...

PHP最常用的2种设计模式工厂模式和单例模式介绍

1.工厂模式 主要作用是降低耦合度。 复制代码 代码如下: abstract class Operation{ abstract public function getValue($nu...

for循环连续求和、九九乘法表代码

for循环的经典例子就是连续求和了:1+2+3+……+100,讲了一个多小时,还是有同学不会。做程序得有思想,有的同学一直敲键盘,也没搞出来。在做这个求和之前,我们要思考一下,求和其实就...