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常用的工具开发整理

php常用的工具开发整理

PHP开发工具及其优缺点 首先,可以用记事本来开发。 记事本每个人的电脑上都有,也就是我们常说的txt文件。把txt这个后缀更改为点PHP就可以了。然后该怎么编辑就怎么编辑。缺点是, 没...

解决GD中文乱码问题

今天仔细研究了下GD的一些相关技术,顺手也研究下GD中文乱码的问题。  使用GD库输出中文字符串,调用imagestring是没有用的。需要使用imagettftext()函数...

php基础知识:函数基础知识

函数,所有的语言都有,所以这里只说重点: 1>定义:php不需要定义返回值类型。 2>函数名是非大小写敏感的,不过在调用函数的时候,通常使用其在定义时相同的形式。 ...

WordPres对前端页面调试时的两个PHP函数使用小技巧

函数esc_js()(过滤 Html 内嵌 JS) 参数 $text (字符串)(必须)要过滤的字符串。 默认值:None 返回值 (字符串)返回过滤后的字符串。 例子 <in...

CodeIgniter与PHP5.6的兼容问题

错误提示: A PHP Error was encountered Severity: Notice Message: Only variable references shou...