大家都遇到过类似问题吗?
刚用PHP的类不久,对此深有感触! 
不知道大家遇到这种问题时都是怎么解决的!希望大家顶上去,别让这贴子早早沉了!

解决方案 »

  1.   

    一个方法同时属于二个类?这是不可能的,即使方法名称相同,但实现的功能肯定会有差别。
    PHP5已经有接口的概念了。这时候可以采用接口来解决,再接口中声明该方法,然后再二个类中分别实现该接口,虽然名称相同,但具体功能代码可能会有些差别。类的划分一般以完成某一类功能为主,最大程度的降低类之间的耦合度。
      

  2.   

    建议参考一下VCL的类结构~~~你会对OO有一个比较实质的认识~~~
      

  3.   

    最近正在烦这个问题,
    因为刚刚接触类不久,以前都是有阿头帮我规划好的.
    现在要自己划分类,还真的比较困难,头都大了. keaizhong(可爱钟) ( )
     zhiin(稚鹰来CSDN交朋友) 
     xuzuning(唠叨) 
     ice_berg16(寻梦的稻草人) 
     mikespook(懒猫开始新生活)这么多位大哥,能给小弟一个划分类的方法吗?[稍微指点一下也行]谢谢大家了.
      

  4.   

    CXMLParser
        |
    CXMLHandler(DeleteNode, AddNode, EditNode, ReadNode)
        |
    CSOAPParser, CXMLConfig, CXMLSQL, ... (各种XML应用)继承是OO的三个特征之一(封装,继承,多态)
      

  5.   

    zhiin (稚鹰来CSDN交朋友)的问题已经比较具体了,类似的苦恼我也有,请上面几位延着这个问题用具体代码来教教我们才好。
      

  6.   

    <?class Counter
    {
    public $thisMonthCount;
    public $lastMonthCount;
    public $thisDayCount;
    public $lastDayCount;
    public $totalCount;
    public $beginDate;
    public $totalDayCount; public $avgCount; public $mysqli; function __construct()
    {
    $this->mysqli = $GLOBALS['mysqli'];
    } /**
     * Function: 分别创建记数器所需的两个表(count, online).  如果已存在则先删除再创建
     */
    private function createTable()
    {
    $this->mysqli->query("drop table if exists `count`");
    $query = "CREATE TABLE `count` (
      `id` tinyint(4) NOT NULL default '1',
      `this_month` int(6) NOT NULL default '0',
      `last_month` varchar(6) NOT NULL default '0',
      `this_day` varchar(5) NOT NULL default '0',
      `last_day` varchar(5) NOT NULL default '0',
      `total` int(11) NOT NULL default '0',
      `begin_date` date NOT NULL,
      `count_date` date NOT NULL,
      PRIMARY KEY  (`id`)
    )";
    $this->mysqli->query($query); $this->mysqli->query("drop table if exists `online`");
    $query = "CREATE TABLE `online` (
      `id` int(11) NOT NULL auto_increment,
      `ip` varchar(16) NOT NULL,
      `time` datetime default NULL,
      PRIMARY KEY  (`id`),
      UNIQUE KEY `ip` (`ip`)
    )";
    $this->mysqli->query($query);
    } /**
     * Function: 初始化记数器, 包括count与online两张表的重新创建
     */
    function reset()
    {
    $this->createTable();
    $this->mysqli->query("insert into `count` (`id`, `this_month`, `last_month`, `this_day`, `last_day`, `total`, `begin_date`, `count_date`)
    values ('1', 0, 0, 0, 0, 0, now(), now())");
    } /**
     * Function: 记数器无条件记数
     */
    private function increment()
    {
    $result = $this->mysqli->query("select day(`count_date`) as `day`, month(`count_date`) as `month` from `count`");
    $row = $result->fetch_assoc();
    $monthOfCountDate = $row['month'];
    $dayOfCountDate = $row['day']; $monthOfDate = date("n");
    $dayOfDate = date("j");
    $monthOfLastDate = date("n", strtotime ("-1 month"));
    $dayOfLastDate = date("j", strtotime ("-1 day"));
    if($monthOfCountDate==$monthOfDate && $dayOfCountDate==$dayOfDate)
    {
    $this->mysqli->query("update `count` set `this_month` = `this_month` + 1, `this_day` = `this_day` + 1, `total` = `total` + 1");
    }
    elseif($monthOfCountDate==$monthOfDate && $dayOfCountDate!=$dayOfDate)
    {
    if($dayOfCountDate==$dayOfLastDate)
    {
    $this->mysqli->query("update `count` set `this_month` = `this_month` + 1, `last_day` = `this_day`, `this_day` = 1, `total` = `total` + 1, `count_date` = now()");
    }
    else
    {
    $this->mysqli->query("update `count` set `this_month` = `this_month` + 1, `last_day` = 0, `this_day` = 1, `total` = `total` + 1, `count_date` = now()");
    }
    }
    elseif($monthOfCountDate!=$monthOfDate)
    {
    if($monthOfCountDate==$monthOfLastDate && $dayOfCountDate==$dayOfLastDate)
    {
    $this->mysqli->query("update `count` set `last_month` = `this_month`, `this_month` = 1, `last_day` = `this_day`, `this_day` = 1, `total` = `total` + 1, `count_date` = now()");
    }
    elseif($monthOfCountDate==$monthOfLastDate && $dayOfCountDate!=$dayOfLastDate)
    {
    $this->mysqli->query("update `count` set `last_month` = `this_month`, `this_month` = 1, `last_day` = 0, `this_day` = 1, `total` = `total` + 1, `count_date` = now()");
    }
    elseif($monthOfCountDate!=$monthOfLastDate)
    {
    $this->mysqli->query("update `count` set `last_month` = 0, `this_month` = 1, `last_day` = 0, `this_day` = 1, `total` = `total` + 1, `count_date` = now()");
    }
    }
    } /**
     * Function: 记录器有条件记数(不在线时才记数, 在线的仅更新活动时间)
     */
    function count()
    {
    $online = new Online();
    if($online->isOnline())
    {
    $online->updateOnlineTime();
    }
    else
    {
    $online->increment();
    $this->increment();
    }
    } /**
     * Function: 初始化记数器的每一个属性
     */
    function init()
    {
    $result = $this->mysqli->query("select *, to_days(now())-to_days(`begin_date`)+1 as `totalDayCount` from `count`");
    $row = $result->fetch_assoc(); $this->thisMonthCount = $row['this_month'];
    $this->lastMonthCount = $row['last_month'];
    $this->thisDayCount = $row['this_day'];
    $this->lastDayCount = $row['last_day'];
    $this->totalCount = $row['total'];
    $this->countDate = $row['count_date'];
    $this->beginDate = $row['begin_date'];
    $this->totalDayCount = $row['totalDayCount'];
    $this->avgCount = round($this->totalCount/$this->totalDayCount);
    } /**
     * Function: 以HTML方式显示记数器各属性
     */
    function printHtml()
    {
    $this->init(); $online = new Online(); echo "<ul class=\"counter\">
    <li>本站总访问: {$this->totalCount}人</li>
    <li>今日访问量: {$this->thisDayCount}人</li>
    <li>昨日访问量: {$this->lastDayCount}人</li>
    <li>本月访问量: {$this->thisMonthCount}人</li>
    <li>上月访问量: {$this->lastMonthCount}人</li>
    <li>本站已运行: {$this->totalDayCount}天</li>
    <li>平均访问量: {$this->avgCount}人</li>
    <li>当前有 {$online->getOnlineCount()} 人在线</li>
    </ul>";
    }
    }?>
      

  7.   

    <?class Online
    {
    public $mysqli; function __construct()
    {
    $this->mysqli = $GLOBALS['mysqli'];
    } /**
     * Function: 更新online表(删除online里3分钟无动作的用户)
     */
    private function updateOnline()
    {
    $this->mysqli->query("delete from `online` where unix_timestamp(now()) - unix_timestamp(`time`) > 3*60");
    } /**
     * Function: 先更新online表, 然后判断用户是否在线, 如在线返回true,不在线返回false
     */
    function isOnline()
    {
    $this->updateOnline();
    $ip = getip();
    $result = $this->mysqli->query("select count(*) as `count` from `online` where `ip` = '$ip'");
    $row = $result->fetch_assoc();
    if($row['count']==1)
    {
    return true;
    }
    else
    {
    return false;
    }
    } /**
     * Function: 先更新online表, 然后返回在线人数
     */
    function getOnlineCount()
    {
    $this->updateOnline();
    $result = $this->mysqli->query("select count(*) as `count` from `online`");
    $row = $result->fetch_assoc();
    return $row['count'];
    } /**
     * Function: 记录在线信息到online
     */
    function increment()
    {
    $ip = getip();
    $this->mysqli->query("insert into `online` (`id`, `ip`, `time`) value ('', '$ip', now())");
    } /**
     * Function: 更新在线IP的活动时间
     */
    function updateOnlineTime()
    {
    $ip = getip();
    $this->mysqli->query("update `online` set `time` = now() where `ip` = '$ip'");
    }
    }?>
      

  8.   

    上面是我在PHP5环境下写的记数器的类~ 显示在线人数~因为是这是本人写的第一个类 所以肯定有很多不足 希望贴出来希望大家能够指正一下!有必要时我再另开一帖!用法:
    <?
    $counter = new Counter;
    //$counter->reset(); 初始化
    $counter->count();
    $counter->printHtml();
    ?>其中的getip()函数为:function getip()
    {
    return !empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
    }
      

  9.   

    我觉得像楼主的app.config之类的应用就不要加到XMLReader里。
    也就是说有特性的东西部要不要和共性的东西放一块,把那些特性的东西和共性的东西混在一块最终将严重影响共性。怎么样用一个构架把许多有共性的问题粘合在一起来应付特性我觉得比较有意义。
    也就是高内聚低耦合。但是要做到挺困难的,经验和技巧一个都不能少。
      

  10.   

    说一下
    SOAP,CONFIG这些不因该继承XML类
    而因该聚合一个XML处理对象
    因为这些类没有使用xml类的方法
      

  11.   

    用户注册,
    登录,
    修改资料,
    应聘职位,
    查看应聘历史,
    保存搜索条件功能,用户类
    职位类
    登陆类
    应聘关系类
    似乎还会有
    企业类
    招聘广告类
    等等
    这些都是model部分
      

  12.   

    这是OO设计的问题,本来就是很复杂的,不会有简单统一的方法。
    可以找OO分析设计的书,UML的书看