zoukankan      html  css  js  c++  java
  • 004-对象——public protected private PHP封装的实例

    <?php
    
    /**
     *public protected private PHP封装的实例
     */
    /*class tv
    {
        private $shengyin;
    
        function __construct()
        {
            $this->shengyin = 20;
        }
    
        public function yaokongqi($anniu, $liang = '')
        {
            switch ($anniu) {
                case "shengyin":
                    $this->shengyin($liang);
                    break;
                case "guandiansi":
                    $this->guandiansi();
                    break;
                case 'huantai':
                    $this->huantai($liang);
                    break;
                case 'jingyin':
                    $this->jinyin($liang);
                    break;
            }
        }
    
        public function shengyin($daxiao)
        {
            $this->shengyin = $daxiao>0?$this->shengyin + $daxiao:$this->shengyin - $daxiao;
            echo "现在的声音是:{$this->shengyin}<br/>";
        }
    
        private function guandiansi()
        {
            echo "关电视";
        }
    
        private function huantai($pindao)
        {
            echo "现在是第{$pindao}频道";
        }
    
        private function jinyin($zhuangtai)
        {
            $zhuangtai == 1 ? "电视静音了" : "打开声音";
        }
    
        public function huodeshengyin()
        {
            return $this->shengyin;
        }
    }
    
    $tv1 = new tv();
    $tv1->yaokongqi('shengyin', 6);
    echo "现在的电视声音是:".$tv1->huodeshengyin();
    */
    
    //最大化的封装,最小化的释放
    /*class db
    {
        private $mysqli;//数据库链接
        private $options;//SQL选项
        private $tableName;//表名
    
        function __construct($tabName)
        {
            $this->tableName = $tabName;
            $this->db();
        }
    
        private function db()
        {
            $this->mysqli = new mysqli('localhost', 'root', '', 'blog');
            $this->mysqli->query('SET NAMES utf8');
        }
    
        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;
    
        }
    
        function order($str)
        {
            $this->options['order'] = "ORDER BY".$str;
            return $this;
        }
    
        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('blog_article');
    $changelInfo=$chanel->fields('art_id,art_title,art_tag')->select();
    echo '<pre>';
    print_r($changelInfo);*/
    
    //public () 本类,子类 ,外部对象都可以执行
    //protected  (受保护的) 本类,子类,可以执行,外部对象不可以执行
    //private (私有的)  只能在奔雷执行,子类与外部对象都不可以调用
    
    /*class a{
        protected function aa(){
            echo 222;
        }
    }
    class b extends a{
        function bb(){
            $this->aa();
        }
    }
    $c=new b();
    $c->bb(); //输出:222;*/
    
    
    /*class a{
        private function aa(){
            echo 222;
        }
    }
    class b extends a{
        function bb(){
            $this->aa();
        }
    }
    $c=new b();
    $c->bb(); //报错:*/
    

      

  • 相关阅读:
    curl -L 跟随跳转
    Http报头Accept与Content-Type的区别
    curl 发送json请求
    IntelliJ 中类似于Eclipse ctrl+o的是ctrl+F12
    Spring AOP
    Windows下Nginx配置SSL实现Https访问(包含证书生成)
    @Retention n. 保留
    Exchanger使用
    Semaphore使用
    UVA12493
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/7940060.html
Copyright © 2011-2022 走看看