Welcome Guest, Not a member yet? Register   Sign In
Model Inheritance
#1

[eluser]adamp1[/eluser]
Someone else posted about this on the forum, but I don't think it was very clear.

What I have is two models, SuperModel and SubModel. SuperModel needs extend Model while SubModel needs to extend SuperModel. I tried the following but it didnt work.

In supermodel.php:
Code:
class SuperModel extends Model
{
  function SuperModel()
  {
    parent::Model();
  }
  // Model Code
}

In subrmodel.php:
Code:
class SubModel extends SuperModel
{
  function SubModel()
  {
    parent::SuperModel();
  }
  // Model Code
}

Upon this not working, the SubModel complained it could not find SuperModel even through I told it to autoload it. I then tried to add a hook linking to the supermodel class, but that had the same problem.

How can I do this?
#2

[eluser]thurting[/eluser]
http://us.php.net/manual/en/function.require-once.php
#3

[eluser]adamp1[/eluser]
How does that help? It could work but it wouldn't stick with the OO approach of MCV of CI?
#4

[eluser]thurting[/eluser]
[quote author="AdamP" date="1195611263"]How does that help? It could work but it wouldn't stick with the OO approach of MCV of CI?[/quote]

Why not?

A 'require' statement is just like an 'import' statement you would use in Java. If anything, explicitly stating the included class files is more OOP compliant and will improve readability and maintainability.

Also, what function do you think CI_Loader is using?
require_once(APPPATH.'models/'.$path.$model.EXT);
#5

[eluser]esra[/eluser]
Take a look at how this was handled for the Nested Sets wiki article. I believe that Thunder UK had to use parent::Model for the parent constructor in the child model. The explanation for this is in the thread associated with the wiki article. For PHP5 only, he apparently treats the parent model as a library.




Theme © iAndrew 2016 - Forum software by © MyBB