开发者

Codeigniter and multiple inheritance

I've been using Codeigniter to construct the front end of a moderately sized application, and have run into an issue with--what I think may be--inheritance in PHP. Here is what I am trying to do:

In following the MVC architecture, I found that I was duplicating a lot of code across models, and decided to create a common Model from which other models could inherit. Simple enough. However, now, I am getting issues with some of the functions which are defined in the common Model class.

Here is a sketch of what I'm doing:

<?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();
    }
  }
}

?>

Here is the subclass:

<?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++) {
            $h开发者_运维百科tml = $this->generateHtml($arrCmds[$i],$did[$i]);
            array_push($arrHtml,$html);
        }

        return $arrHtml;
    }        

Is there a problem using multiple inheritance in codeigniter? I am fairly new to PHP and codeigniter.

Thanks for looking.


I don't see where is the multiple inheritance there.

I'm also working with codeigniter, and I had the need to subclass it's Controller so all my Controllers can descend from mine and not from CodeIgniter's directly.

CodeIgniter has native methods for extending it's classes with your own. Or you could open the model.php file (in system/libraries/) and at the top of the file, right after the if (!defined ...), you could add the code of your ManageModel class

Also here's a link for extending the model http://www.askaboutphp.com/50/codeigniter-extending-the-native-model-and-make-it-your-own.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜