【发布时间】:2010-07-26 17:53:14
【问题描述】:
我一直在使用 Codeigniter 构建一个中等大小的应用程序的前端,并且遇到了一个问题——我认为可能是——PHP 中的继承。这是我想要做的:
在遵循 MVC 架构时,我发现我正在跨模型复制大量代码,因此决定创建一个通用模型,其他模型可以从该模型继承。很简单。但是,现在,我遇到了一些在通用模型类中定义的函数的问题。
这是我正在做的事情的草图:
<?php
/**
* Common Model
*
*/
class DeviceModel extends Model {
function DeviceModel() {
parent::Model();
}
public function getDeviceId($d) { // this is just example code. }
public function getDeviceInfo($id) {
$selectStmt = "SELECT BLAH, BLAH2 FROM YADDAYADDA...";
$query = $this->db->query($selectStmt, array($id));
if ($query->num_rows() > 0) {
return $query->result();
}
}
}
?>
这是子类:
<?php
require_once('devicemodel.php');
class ManageModel extends DeviceModel {
function ManageModel() {
parent::DeviceModel();
}
function getDropDownList($parkId,$tableName,$userclass) {
$arrCmds = array();
$arrHtml = array();
$deviceInfo = parent::getDeviceInfo($parkId);
$did = parent::getDeviceId($deviceInfo);
foreach ($deviceInfo as $device) {
$cmds = $this->getDeviceCommands($device->dtype,$tableName,$userclass);
array_push($arrCmds,$cmds);
}
//
// **After the refactor, I am receiving Undefined Offsets for this loop.**
//
for ($i=0; $i<sizeof($arrCmds); $i++) {
$html = $this->generateHtml($arrCmds[$i],$did[$i]);
array_push($arrHtml,$html);
}
return $arrHtml;
}
在codeigniter中使用多重继承有问题吗?我对 PHP 和 codeigniter 还很陌生。
感谢收看。
【问题讨论】:
-
我已经在 codeigniter 中做过这种事情,而且从来没有引起过问题。这可能是 getDeviceInfo 中的逻辑问题......随机想法/我的约定将 getDeviceInfo 调用为 $this->getDeviceInto 而不是使用父 ref 而不是使用 array_push 使用 arrCmds[] = $cmds;
标签: php inheritance codeigniter