Welcome Guest, Not a member yet? Register   Sign In
Extending CI_Model causing $this->load->model() to create an unnecessary MY_Model object instance
#1

[eluser]mripod[/eluser]
I'm experiencing some weird behaviour when trying to extend CI_Model using CI Reactor v2.0.1. Create the files below and the behaviour is easily reproducible.

The $this->load->model(‘example’) method is creating a MY_Model object *AND* an Example object. If you look at the controller output you’ll see that that get_class($this) in the constructor of MY_Model is being triggered twice. Once when an unnecessary instance of MY_Model is created, then again when the actual Example object to be returned is created. The $this->load->model(‘example’) method shouldn’t need to create a MY_Model object instance. As it stands putting any code in the MY_Model constructor will cause it to get triggered twice upon the first load of a new model.

Code:
<?php
// application/core/MY_Model.php
class MY_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        echo '<p>' . get_class($this) . '</p>';
    }
}

Code:
&lt;?php
// application/models/Example.php
class Example extends MY_Model
{

}

Code:
&lt;?php
// application/controllers/welcome.php
class Welcome extends CI_Controller {
    public function index()
    {
        $this->load->model('example');
    }
}

Code:
// Hitting the welcome controller outputs the following:
MY_Model
Example
#2

[eluser]InsiteFX[/eluser]
And in what directory did you place your MY_Model?

InsiteFX
#3

[eluser]mripod[/eluser]
I added a comment above each code snippet showing where the file is located. MY_Model.php is inside application/core
#4

[eluser]InsiteFX[/eluser]
Note:

Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first. If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first. To make it short: the order in which the classes are defined is important.

InsiteFX
#5

[eluser]mripod[/eluser]
My question isn't about how to extend a class. My question is why is CI creating an instance of MY_Model before it creates an instance of the Example model that I'm loading? It shouldn't need to.
#6

[eluser]InsiteFX[/eluser]
If you read what I wrote above!

How our you going to extend example from MY_Model if MY_Model has not been loaded?

The class that you are extending from has to be Initiated before you can use it!

So:

MY_Model
Xample extends MY_Model

Simple you can not call a Class until you have created a new instance of that class.

InsiteFX
#7

[eluser]mripod[/eluser]
It appears the code in my initial post was slightly incorrect, so I've updated it, as I believe this may have caused some confusion causing you to take it off on a tangent. Please take a look over it again, as my problem has nothing to do with understanding how to extend classes, but rather how $this->load->model() is creating an unnecessary MY_Model object instance.

The $this->load->model('example') method is creating a MY_Model object *AND* an Example object. If you look at my controller output you'll see that that get_class($this) in the constructor of MY_Model is being triggered twice. Once when an unnecessary instance of MY_Model is created, then again when the actual Example object to be returned is created. The $this->load->model('example') method shouldn't need to create a MY_Model object instance. As it stands putting any code in the MY_Model constructor will cause it to get triggered twice upon the first load of a new model, which is odd behaviour.
#8

[eluser]InsiteFX[/eluser]
Everytime that MY_Model is loaded it is going to fire your Constructor.

Hence CodIgniter loads MY_Model MY_Model
Then you our extending Example Constructor is fired again Example

So you end up with
MY_Model
Example

May be I am wrong but thats what I think is happening, I' ll test this out later and see what I am getting.

Also in a Model you do not need the Constructor unless you are going to be initalizing data.

You can also take a look at system/core/loader.php this is the file that loads everything.

InsiteFX
#9

[eluser]mripod[/eluser]
I was intending to add some behaviour into the MY_Model constructor that relies upon get_class($this), but this issue means a new MY_Model object instance gets unnecessarily created by $this->load->model() before my Example model object is created. Obviously I can work around the issue by checking the result of get_class($this) before proceeding to do any more work in the constructor, but it felt like a bug which is why I posted here. Maybe I'll just report on Bit Bucket.
#10

[eluser]InsiteFX[/eluser]
If I remember right I think you have to create an Initalize method because you can not pass data into a Model Constructor unless they have changed this now.

This is how it was done:
Code:
class Your_model extends CI_Model {

    // Class Variables.
    // --------------------------------------------------------------------
    private $params = array();

    // --------------------------------------------------------------------

    /**
     * __construct
     *
     * PHP 5+    Constructor.
     *
     * @access    public
     * @return    void
     */
    public function __construct()
    {
        parent::__construct();
    }

    // --------------------------------------------------------------------

    /**
     * Function    Name
     *
     * Description:
     *
     * @access    public
     * @return    void
     */
    public function initialize($param1, $param2 = NULL)
    {
        $this->params = $param1;
    }

}

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB