Heim  >  Artikel  >  php教程  >  php学习笔记之面向对象编程_php技巧

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

PHP中文网
PHP中文网Original
2016-05-17 09:03:04995Durchsuche

一个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 ( &#39;localhost&#39;, &#39;root&#39;, &#39;&#39;, &#39;hdcms&#39; ); 
        $this->mysqli->query("SET NAMES GBK"); 
    } 
    public function fields($fildsArr) { 
        if (empty ( $fildsArr )) { 
            $this->options [&#39;fields&#39;] = &#39;&#39;; 
        } 
        if (is_array ( $fildsArr )) { 
            $this->options [&#39;fields&#39;] = implode ( &#39;,&#39;, $fildsArr ); 
        } else { 
            $this->options [&#39;fields&#39;] = $fildsArr; 
        } 
        return $this; 
    } 
    public function order($str) { 
        $this->options [&#39;order&#39;] = "ORDER BY " . $str; 
        return $this; 
    } 
    public function select() { 
        $sql = "SELECT {$this->options[&#39;fields&#39;]} FROM {$this->tableName}  {$this->options[&#39;order&#39;]}"; 
        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 ( &#39;id,cname,cpath&#39; ) 
    ->select (); 
echo "<pre class="brush:php;toolbar:false">"; 
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 私有的:只能本类执行,子类与外部对象都不可调用

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn