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

yipeiwu_com5年前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 计算两个时间戳相隔的时间的函数(小时)

这个是可以具体到小时的php代码复制代码 代码如下:/* Author: 杨宇 yangyu@sina.cn */ //输入两个时间戳,计算差值,也就是相差的小时数,如返回2:10,则表...

PHP的PDO常用类库实例分析

本文实例讲述了PHP的PDO常用类库。分享给大家供大家参考,具体如下: 1、Db.class.php 连接数据库 <?php // 连接数据库 class Db {...

php获取某个目录大小的代码

大致程序思想就是使用递规来计算目录占用空间多少, 然后再把这个占用空间的值写进文本文件里, 那么只要访问这个txt文件就知道占用了多少空间, 不用频繁获取而读磁盘, 节省资源. 每次用户...

php基础知识:类与对象(1)

php基础知识:类与对象(1)

类的定义:   以关键字 class 开头,后面跟着类名,可以是任何非 PHP 保留字的名字。后面跟着一对花括号,里面包含有类成员和方法的定义。伪变量$this可以在...

PHP PDOStatement:bindParam插入数据错误问题分析

废话不多说, 直接看代码:复制代码 代码如下:<?php$dbh = new PDO('mysql:host=localhost;dbname=test', "test");$qu...