php面向对象深入理解(一)

面向对象(Object Oriented Programming,OOP)的基础知识:

第一个例子:

  类Test.class.php

<?php
class Test{
    public $a=1;
    protected $b=2;
    private $c=3;
    public function fun1(){
        //echo "fun1";
        //调用fun2
        $this->b=789;
        $this->fun2();
        //echo $this->c;
    }
    protected function fun2(){
        //echo "fun2";
        echo $this->b;
    }
    private function fun3(){
        echo "fun3";
    }
}

  引用类 text.php

require 'Test.class.php';
$ob=new Test();
echo $ob->a;
echo "<hr/>";
$ob->fun1();

第二个例子(构造函数):

class Person{
     function __construct($name,$sex)
     {
          $this->name=$name;
          $this->sex=$sex;
     }                                              //定义类的属性
     function showName()
     {
         echo $this->name;              //定义类的showName方法
     }
     function showSex()
    {
         echo $this->sex;                 //定义类的showSex方法
    }
 }
 
 class Worker extends Person{
     function __construct($name,$sex,$job)
     {
           parent::__construct($name,$sex);   //调用Person构造                           函数,继承Person
     }
 }

 第三个例子(数据库的例子):

  类Db.class.php

<?php
class DB{
    protected $conn="";
    /*
     * 作用:连接数据库,打开 设置交互字符集,选择数据库
     * 参数:host  username  password dbName charset
     * 返回值:bool
     */
    function connect($host,$username,$password,$dbName,$charset='utf8'){
        $link=mysql_connect($host,$username,$password);
        $this->conn=$link;
        if(is_resource($link)){
            mysql_set_charset($charset);
            $re=mysql_select_db($dbName);
            if($re){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    /*
     * 作用:执行sql语句
     * 参数:sql语句
     * 返回值:update delete 返回影响记录条数
     *          insert 返回主键id值
     *          select 返回二维数组
     */
    function query($sql){
        $re=mysql_query($sql);
        if($re){
            //判断sql语句的类型
            if(preg_match("/^update|^delete/i",$sql)){
                return mysql_affected_rows();
            }else if(preg_match("/^insert/i",$sql)){
                return mysql_insert_id();
            }else if(preg_match("/^select/i",$sql)){
                //返回二维数组
                $arr=array();
                while($row=mysql_fetch_assoc($re)){
                    $arr[]=$row;
                }
                return $arr;
            }else{
                return $re;
            }
        }else{
            return false;
        }
    }
    
    /*
     * 作用:关闭数据库连接
     * return:bool
     */
    function close(){
        return mysql_close($this->conn);
    }
}

  引用类Db.php

<?php
header("content-type:text/html;charset=utf-8");
require 'Db.class.php';
$ob=new Db();
$ob->connect("localhost", 'root', 'root', 'cms');
$arr=$ob->query("select * from news limit 5");
var_dump($arr);
$ob->close();

 

转载于:https://www.cnblogs.com/wangjinke/p/4776894.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值