您的位置:首页 > 编程语言 > PHP开发

PHP面向对象------静态属性和方法

2013-04-25 19:23 239 查看
封装的数据库类,改写成静态属性和方法来访问

<?php

class db

{

static private $hostname;

static private $user;

static private $pass;

static private $dbname;

static private $linkflag;

static private $charset;

static public function __construct($host,$user,$pass,$dbname,$charset)

{

self::$hostname=$host;

self::$user=$user;

self::$pass=$pass;

self::$dbname=$dbname;

self::$charset=$charset;

self::$linkflag=mysql_connect(self::$hostname,self::$user,self::$pass);

mysql_select_db(self::$dbname,self::$linkflag) or die('连接失败!');

mysql_query("set names ".self::$charset);

}

static public function getAll($sql){

$result = mysql_query($sql);

$rows = array();

while($row=mysql_fetch_assoc($result)){

$rows[] = $row;

}

return $rows;

}

static public function getOne($sql){

$result = mysql_query($sql);

$row = mysql_fetch_assoc($result);

return $row;

}

static public function __destruct()

{

mysql_close(self::$linkflag);

}

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: