PHP面向对象编程的基础知识讲解
2013-09-04 21:47
591 查看
我们今天为大家介绍的是关于下面我们就通过实例来说明使用PHP面向对象编程的实际意义和应用方法。 我们通常在做一个有数据库后台的网站的时候,都会考虑到程序需要适用于不同的应用环境。和其他编程语言有所不同的是,在PHP中,操作数据库的是一系列的具体功能函数(如果你不使用ODBC接口的话)。这样做虽然效率很高,但是封装却不够。如果有一个统一的数据库接口,那么我们就可以不对程序做任何修改而适用于多种数据库,从而使程序的移植性和跨平台能力都大大提高。 PHP面向对象编程的完成,需要进行对象封装,也就是编写类。我们可以通过生成一个新的SQL类实现对数据库的简单封装。例如:
< ? class SQL { var $Driver; //实际操作的数据库驱动子类 var $connection; //共用的数据库连接变量 function DriverRegister($d) { if($d!="") { $include_path = ini_get("include_path"); $DriverFile = $include_path."/".$d.".php"; //驱动的存放路径必须在PHP.ini文件中设定的INCLUDE_PATH下 if( file_exists( $DriverFile)) //查找驱动是否存在 { include($DriverFile); $this->Driver = new $d(); // 根据驱动名称生成相应的数据库驱动类 return true; } } return false; //注册驱动失败 } function Connect($host,$user,$passwd,$database)//连接数据库的函数 { $this->Driver->host=$host; $this->Driver->user=$user; $this->Driver->passwd=$pas swd; $this->Driver->database=$d atabase; $this->connection = $this->Driver->Connect(); } function Close()//关闭数据库函数 { $this->Driver->close($this->connection); } function Query($queryStr)//数据库字符串查询函数 { return $this->Driver->query($queryStr,$this->connection); } function getRows($res)//查找行 { return $this->Driver->getRows($res); } function getRowsNum($res)//取得行号 { return $this->Driver-> getRowsNum ($res); } } ? > 希望以上介绍的PHP面向对象编程的相关知识对大家有所帮助。
< ? class SQL { var $Driver; //实际操作的数据库驱动子类 var $connection; //共用的数据库连接变量 function DriverRegister($d) { if($d!="") { $include_path = ini_get("include_path"); $DriverFile = $include_path."/".$d.".php"; //驱动的存放路径必须在PHP.ini文件中设定的INCLUDE_PATH下 if( file_exists( $DriverFile)) //查找驱动是否存在 { include($DriverFile); $this->Driver = new $d(); // 根据驱动名称生成相应的数据库驱动类 return true; } } return false; //注册驱动失败 } function Connect($host,$user,$passwd,$database)//连接数据库的函数 { $this->Driver->host=$host; $this->Driver->user=$user; $this->Driver->passwd=$pas swd; $this->Driver->database=$d atabase; $this->connection = $this->Driver->Connect(); } function Close()//关闭数据库函数 { $this->Driver->close($this->connection); } function Query($queryStr)//数据库字符串查询函数 { return $this->Driver->query($queryStr,$this->connection); } function getRows($res)//查找行 { return $this->Driver->getRows($res); } function getRowsNum($res)//取得行号 { return $this->Driver-> getRowsNum ($res); } } ? > 希望以上介绍的PHP面向对象编程的相关知识对大家有所帮助。
相关文章推荐
- PHP:面向对象编程基础知识
- redis启动后杂项基础知识讲解
- php基础知识的灵活使用
- 新手必看的PHP学习入门的一些基础知识
- C++ Virtual基础知识讲解
- PHP基础知识2
- PHP入门基础知识介绍
- php日期和时间基础知识--Unix时间戳
- 入门:PHP编程“数组”的基础知识
- PHP 基础小知识
- Python:pygame游戏编程之旅七(pygame基础知识讲解1)
- PHP 基础知识四(正则表达式)
- php基础知识【函数】(9)数学和对象类函数
- Redis启动后杂项基础知识讲解
- php基础知识:控制结构
- php基础知识:类与对象(2) 自动加载对象
- PHP基础知识构造函数
- PHP基础知识回顾
- Redis启动后杂项基础知识讲解(五)
- php语言基础知识