偶写的一个功能比较弱的。
这是刚写,很很很不完善,主要是想学下这种思想。
一个简单的在线用户查询类,继承一个超简单的数据库操作类。
<?
/**
 *  数据库操作类
 *  Last Modify: 2004-08-27
 *
 * @author elliott [at] 2004-08-27
 * @version 1.0.0
 */
 
class DBop
{
var $dbHost;
var $dbUser;
var $dbPassword;
var $dbName; var $dbLink;
var $query; /**
 *  构造函数
 *  Last Modify: 2004-08-27
 *
 * @param $host 数据库地址
 * @param $user 数据库使用者用户名
 * @param $password 数据库使用者密码 
 * @param $table 数据库表名
 */
function DBop($host, $user, $password, $dbName)
{
$this->dbHost = $host;
$this->dbUser = $user;
$this->dbPassword = $password;
$this->dbName = $dbName;
} /**
 * 暂时性方法
 * php类使用方法不熟悉,不知子类如何调用父类构造函数
 * :~(
 */
function setDBop($host, $user, $password, $dbName)
{
$this->dbHost = $host;
$this->dbUser = $user;
$this->dbPassword = $password;
$this->dbName = $dbName;
} /**
 *  连接数据库
 *  Last Modify: 2004-08-27
 */
function getConn()
{
$this->dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPassword) or die(mysql_error());

mysql_select_db($this->dbName) or die(mysql_error());
} /**
 *  查询数据库
 *  Last Modify: 2004-08-27
 */
function executeQuery($str)
{
//echo $str."<br>";
$this->query = mysql_query($str) or die(mysql_error());
}

/**
 * 取得结果集中的数据并生成数组形式
 * @param null
 * @return 成功返回数组,否则返回FALSE
 */
function recordArray()
{
$rows = mysql_fetch_array($this->query);
if(!is_array($rows))
{
echo "取得结果集失败!";
return false;
}
return $rows;
} /**
 * 取得查询记录的列数
 * @param null
 * @return 取得的列数目
 */
function getNumRows()
{
return mysql_num_rows($this->query);
}
}?>

解决方案 »

  1.   

    <?php
    /**
     * 检测在线用户类
     *  Last Modify: 2004-09-06
     *
     * @author elliott [at] 2004-09-06
     * @version 1.0.0
     */include("DBop.inc.php");class GetOnlineUser extends DBop
    {
    var $checkSecond = 300;
    var $userIP;

    var $dbTable;
    var $dbRows;

    /**
     * 设置存放用户在线信息表
     * @param $dbTable 表名
     * @return null
     */
    function GetOnlineUser($host, $user, $password, $dbName)
    {
    $this->setDBop($host, $user, $password, $dbName);
    $this->getConn();
    } /**
     * 设置检测用户在线秒数
     * @param sec
     * @return null
     */
    function setCheckSec($sec)
    {
    $this->checkSecond = $sec;
    }

    function setDBtable($str)
    {
    $this->dbTable = $str;
    } /**
     * 取得客户端IP,并以此做为判断的根据
     * @param null
     * @return null
     */
    function getIP()
    {
    return $this->userIP = getEnv("REMOTE_ADDR");
    }

    /**
     * 设置检测存活区间时间,默认为5分钟
     * @param $second 秒数
     * @return null
     */
    function setCheckSecond($second)
    {
    $this->checkSecond = $second;
    } /**
     * 判断在线用户
     * @param null
     * @return null
     */
    function check()
    {
    //删除大于一小时的记录
    $this->executeQuery("DELETE FROM `$this->dbTable` WHERE (UNIX_TIMESTAMP(NOW())- UNIX_TIMESTAMP(lastLoginTime)) > '3600'");
    $time = date("Y-m-d H:i:s");
    $ip = $this->getIP();
    $this->executeQuery("SELECT * FROM `$this->dbTable` WHERE ip = '$ip'"); if($this->getNumRows())
    {
    $this->executeQuery("UPDATE `$this->dbTable` SET lastLoginTime = '$time' WHERE ip = '$ip'");
    }
    else
    {
    $this->executeQuery("INSERT INTO `$this->dbTable` (ip, lastLoginTime) VALUES ('$ip', '$time')");
    }
    $this->executeQuery("SELECT COUNT(*) AS onlineNum FROM `$this->dbTable` WHERE (UNIX_TIMESTAMP(NOW())- UNIX_TIMESTAMP(lastLoginTime)) <= $this->checkSecond"); $this->dbRows = $this->recordArray(); return $this->dbRows['onlineNum'];
    } function display($rows)
    {
    echo $rows['onlineNum'];
    }
    }/*
    useage:$online = new GetOnlineUser("localhost", "elliott", "working@mysql", "only");
    $online->setDBtable($x['online']);
    $onlineNum = $online->check();
    echo $onlineNum;*/?>
      

  2.   

    里面的oo最好换成 xuzuning(唠叨) 老大的代码..
      

  3.   

    还有就是楼主,你的变量的引用最好换成方法引用的方式..这样更符合oop和ood的思想..
      

  4.   

    呵呵,唠叨老大果然高明哦。。一时竟然没想到。。to lzkd(浪子快刀):
    是啊我开始学的时候在网上看到的都是什么车啊轮啊。。汗看得我迷迷糊糊的。
      

  5.   

    to tod204(八十年代):
    恩,谢谢你的指点!
      

  6.   

    to  unixdotnet(concinnity) :
    恩,在实现某个具体功能的时候,对象与CLASS的代码加起来的代码行一般比为实现该功能而写一段非OO代码行多吧?(只是实现类中的一个功能)
    当然,在一大个项目里多次使用同一个类,总代码行变多后,类的代码行数也就不值一提了..这时候用OO就显得精简好多了...
    可能我语言表达上有点欠缺了..呵呵,今年高考语文成绩低于本科线呢...补习ing..