views:

108

answers:

1

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++) {
            $html = $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.

A: 

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

Aldo
"Or you could open the model.php file (in system/libraries/)" You should NEVER EVER modify files in anything other then the application folder. One of the best features of codeigniter is a the ability to drag and drop for application folder into newer versions of ci (try upgrading in symfony it's a pain). If you modify something in system/libraries you might miss a bug when upgrading.
Ken Struys
I know that if you have modified files it might be a hassle when upgrading, but that's one method of creating your own classes. The other one, which is also the proper way to do it, as noted in the answer above is throught it's native extending capabilities
Aldo