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 SQLite类

复制代码 代码如下:<? /** * SQLite类 * 2009-5-6 * 连万春 * */ class SQLite {     //...

PHP 开发环境配置(Zend Studio)

PHP 开发环境配置(Zend Studio)

  安装完成后运行程序 在Help菜单中有Register选项进行注册。 提供注册码如下: Username: zendstudio.netSerial Nu...

详解WordPress开发中的get_post与get_posts函数使用

get_post() 在一般主题制作时,get_post()函数我们一般很少会用到,但因为后面会讲到get_posts(),所以我们不得不先讲一下这个单数形式。这个函数的主要作用是,将...

php读取远程gzip压缩网页的方法

php读取远程gzip压缩网页的方法

今天在调取一家商城的页面信息时候,使用file_get_contents抑或curl: 复制代码 代码如下: $url = 'http://www.xxx.com/21/?ty...

一道求$b相对于$a的相对路径的php代码

php面试题的题目: $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php'; //计算出 $b 相对于 $a 的相对路径应该是 ../../c/d...