PHP中的继承

时间:2013-09-04 16:39:00

标签: php inheritance

class Users
{
    function inWork($uid) 
    {
        $q = 'SELECT in_work FROM user_activity WHERE user_id=? LIMIT 1';

        $stmt = $this->pdo->prepare($q);
        $params = array($uid);
        $count = $stmt->execute($params);

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if($row['in_work'] == 0){
            $status = 'Not Working';
        } elseif($row['in_work'] == 1){
            $status = 'Working';
        }

        return $status;
    }

}

并且

class Call_log extends Users
{
    function getUserCalls($uid)
    {
        print $this->inWork($uid);
    }
}

$call_log = new Call_log;
print $call_log->inWork(n); 

来自客户端代码,正常工作和行为,即根据数据库中的整数值显示“工作”或“不工作”。

Call_log内调用相同的方法,但它无法正常工作或我得到意外的结果。

该方法仅输出“不工作”?

我的Call_log类继承了用户的所有方法,因此从inWork($uid)内调用getUserCalls($uid)应该不是问题?或者至少这就是我的想法。

背景:

SELECT in_work FROM user_activity WHERE user_id=32 LIMIT 1;
+---------+
| in_work |
+---------+
|       1 |
+---------+

SELECT in_work FROM user_activity WHERE user_id=2 LIMIT 1;
+---------+
| in_work |
+---------+
|       0 |
+---------+

编辑:长版:

function getUserCalls($uid){

    $stmt = $this->pdo->prepare("CALL select_calls_by_extn(?)");
    $stmt->bindParam(1, $uid, PDO::PARAM_INT, 5);
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $stmt->execute();

    $rows = $stmt->fetchAll();

    if(!$rows){
        throw new Exception('We couldn\'t find any records for that name.');
    }

    foreach ($rows as $row){

        if($row['in_work'] == 0){
            $status = 'Not Working';
        } elseif($row['in_work'] == 1){
            $status = 'Working';
        }   

        //$status = $this->inWork($uid);


        print '<tr><td>'.$row['ext_num'].'</td><td>'.$row['username'].'</td><td>'.$status.'</td><td>'.$row['avg_in'].'</td><td>'.$row['avg_out'].'</td><td>'.$row['calls_in'].'</td><td>'.$row['calls_out']."</td></tr>\n";
    }
}

我希望''注释'$ status变量将使用继承来获取值,从而减少代码重复。

1 个答案:

答案 0 :(得分:0)

你有正确的概念。从inWork内拨打getUserCalls完全有效。为了确保您的查询/存储函数不是问题,我建议硬编码两个不同的值,然后执行方法来测试您是否得到正确的结果。

class Users
{
    function inWork($uid) 
    {
        // instead of getting data from the database, let's force the data 
        // to look as if the database has provided us the data. 
        // If user id 32 is passed, flag staticData to 1
        $staticData = ($uid === 32) ? 1 : 0;
        $row = array('in_work'=>$staticData);

        if($row['in_work'] == 0){
            $status = 'Not Working';
        } elseif($row['in_work'] == 1){
            $status = 'Working';
        }

        return $status;
    }
}


class CallLog extends Users
{
    function getUserCalls($uid)
    {
        echo $this->inWork($uid);
    }
}


$callLog = new CallLog();
print $callLog->inWork(2); // Should print Not Working
print $callLog->getUserCalls(2); // Should print Not working

print $callLog->inWork(32); // Should print Working
print $callLog->getUserCalls(32); // Should print working

使用您使用的两个不同语句在var_dumpprint_r$rows调试您的应用程序:

  1. select .... from ....
  2. CALL select_calls_by_extn
  3. 你应该得到相同的结果,var_dump的结果将允许你看看变量发生了什么,以及SQL的结果是否是实际问题。

    从我读到的,你的继承理论是好的。