inheritance - Inheritence in PHP -
class users { function inwork($uid) { $q = 'select in_work user_activity 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; } } and
class call_log extends users { function getusercalls($uid) { print $this->inwork($uid); } } and
$call_log = new call_log; print $call_log->inwork(n); from client code, works , behaves correctly i.e displays 'working' or 'not working' accordingly based on integer value database.
a call same method within call_log , doesn't work correctly or unexpected results.
the method outputs 'not working' only?
my call_log class inheriting methods users , calling inwork($uid) within getusercalls($uid) shouldn't problem? or @ least that's thought.
background:
select in_work user_activity user_id=32 limit 1; +---------+ | in_work | +---------+ | 1 | +---------+ select in_work user_activity user_id=2 limit 1; +---------+ | in_work | +---------+ | 0 | +---------+ edit: long version:
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 records name.'); } foreach ($rows $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"; } } i hoping 'commented-out' $status variable take on value using inheritence cutting down on code duplication.
you have right concept. calling inwork within getusercalls valid. ensure queries/stored functions aren't issue, i'd recommend hard coding 2 different values , executing methods test if correct results.
class users { function inwork($uid) { // instead of getting data database, let's force data // if database has provided data. // if user id 32 passed, flag staticdata 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 debug application doing var_dump or print_r on $rows 2 different statements have used:
select .... ...., andcall select_calls_by_extn
you should identical results , results of var_dump allow see happening variables , if results of sql actual issue.
from read, theory of inheritance alright.
Comments
Post a Comment