PHP面向对象----继承特性
2013-04-25 19:23
155 查看
将数据库常用的操作(连接数据库,获得所有数据,获得一条记录,获得一列记录,获得一条索引数组,获得一条关联数组)都给封装到db.class.php里面,谁需要谁继承这个类
注意:提交封装的类文件
<?php
class db
{
private $hostname;
private $user;
private $pass;
private $dbname;
private $linkflag;
private $charset;
public function __construct($host,$user,$pass,$dbname,$charset)
{
$this->hostname=$host;
$this->user=$user;
$this->pass=$pass;
$this->dbname=$dbname;
$this->charset=$charset;
$this->linkflag=mysql_connect($this->hostname,$this->user,$this->pass);
mysql_select_db($this->dbname,$this->linkflag) or die('连接失败!');
mysql_query("set names ".$this->charset);
}
public function getAll($sql){
$result = mysql_query($sql);
$rows = array();
while($row=mysql_fetch_assoc($result)){
$rows[] = $row;
}
return $rows;
}
public function getOne($sql){
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
return $row;
}
public function __destruct()
{
mysql_close($this->linkflag);
}
}
?>
注意:提交封装的类文件
<?php
class db
{
private $hostname;
private $user;
private $pass;
private $dbname;
private $linkflag;
private $charset;
public function __construct($host,$user,$pass,$dbname,$charset)
{
$this->hostname=$host;
$this->user=$user;
$this->pass=$pass;
$this->dbname=$dbname;
$this->charset=$charset;
$this->linkflag=mysql_connect($this->hostname,$this->user,$this->pass);
mysql_select_db($this->dbname,$this->linkflag) or die('连接失败!');
mysql_query("set names ".$this->charset);
}
public function getAll($sql){
$result = mysql_query($sql);
$rows = array();
while($row=mysql_fetch_assoc($result)){
$rows[] = $row;
}
return $rows;
}
public function getOne($sql){
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
return $row;
}
public function __destruct()
{
mysql_close($this->linkflag);
}
}
?>
相关文章推荐
- php部分--面向对象三大特性-封装(另加连续调用的一个例子)、继承(重写、重载的例子)、多态;
- PHP学习笔记之继承(面向对象三大特性之一)
- PHP入门教程之面向对象的特性分析(继承,多态,接口,抽象类,抽象方法等)
- PHP入门教程之面向对象的特性分析(继承,多态,接口,抽象类,抽象方法等)
- php面向对象三大特性——继承
- 面向对象的特性:封装继承多态
- PHP面向对象——三大基本特性与五大基本原则
- php面向对象 继承
- php继承后构造函数的特性
- C#的面向对象特性之继承
- PHP学习笔记---封装(面向对象三大特性之一)
- php的学习笔记之面向对象(四)继承补充
- PHP没有多继承的特性
- JavaScript的相关继承笔记以及使用外部库实现JavaScript的面向对象特性
- java笔记—封装、继承、多态[面向对象的三大特性]
- java:面向对象的三大特性之一:继承
- java 面向对象三大特性(封装,继承,多态)以及抽象、接口的介绍
- PHP面向对象的特性
- PHP面向对象的继承
- 面向对象三大特性:继承、封装、多态