4

I'm trying to write my own MY_Model base class but I'm facing a weird problem:

/core/MY_Model.php

 function __construct() { if ( !empty($this->table)) { // query db, etc. } else { // even though that I set $this->table value // in the child class, I always ended up here // it's always empty!!!! log_message('error', 'some error message'); } // ... } } 

/models/test_model.php

 function __construct() { parent::__construct(); } // ... } 

even though that I set $this->table value in the child class, I always ended up finding $table value empty in MY_Model class, it's always empty!!!! any hint please?!

7
  • As far as I see you try to override property $table, right? I'm asking just to make clear if I understand your question well. Commented Jun 4, 2012 at 13:06
  • PLB: Not exactly, I'm trying to pass table name to the parent class to make it query database for some results. I thought redefining $table property in child class will do that. But it's not updating parent class $table property. If do a var_dump() in parent constructor $table property is still empty. thank u. Commented Jun 4, 2012 at 13:09
  • That's weird, I do the exact same thing in my MY_Model but $table is set as it should in the parent class... Commented Jun 4, 2012 at 13:12
  • @Stephanie strange. I've even tested your script, because it should be working and it really does what you expect. Commented Jun 4, 2012 at 13:14
  • weireddddddddddd! thank you so much anyway ;) Commented Jun 4, 2012 at 13:49

3 Answers 3

11

Unless you need something specific/special - you should just use Jamie Rumbelows "MY_Model" - it will probably be better than anything you could write (or me) - and allows you to focus writing other code. No need to re-invent the wheel.

You can get the code from GitHub here

And here is an awesome tutorial that shows you how to use the MY_Model in your application.

Honestly - give it a go - you wont regret it.

Sign up to request clarification or add additional context in comments.

Comments

2

In your model constructor, do this:

class Test_model extends MY_Model{ public function __construct() { parent::__construct(); $this->table = 'test_model'; } //... } 

Comments

1

You shouldn't try to override the $table variable in the child class. What you're doing is:

  • CI_MODEL contains:
    • protected $table, of own scope.
  • MY_Model contains:
    • protected $table, of own scope. This overrides the inherited variable $table.

When you override it in the child class, it won't bubble up to the parent class, cause the $table will become a part of the child class only.

What you need to do is remove the declaration of the $table variable in the child:

/models/test_model.php <?php class Test_model extends MY_Model { function __construct() { parent::__construct(); $this->table = 'test_models'; } // ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.