CodeIgniter Forums
Problem class objects and CI object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem class objects and CI object (/thread-10778.html)



Problem class objects and CI object - El Forum - 08-12-2008

[eluser]bramb79[/eluser]
Anyone can offer me some advise on the following:

I am working on a project where I want to set my up data objects along the line of
http://ellislab.com/forums/viewthread/49403/P15/#239838.

So I have a for example:
Code:
class Usermodel extends MY_Model {
    function __construct() {
    }
    More code...    
}

class User extends MyClass {
    function __construct() {
    }

    More code...
}

I can then create an instance of a User (new User()), put some values in the User object, for example a "name", and then call $this->getName() on the User object to return me the username (in an appropriate format).

I also want to be able to reference other models and functions from within the CI object. For the User example that would probably mean something like:
Code:
$this->CI->load->model('groupmodel');
$this->groupmodel->getUserGroup($this->id);
Instead of calling another function this might also be a database query.

Now my problem is, is that as soon as I load the CI object in the User class, in MyClass or extend MyClass with Model (which it is most related too), the CI object (which is quite large) becomes part of the class. When I create a new instance of that class. For example:
Code:
$user = new User();
The $user object now also contains the entire CI object which is something I do not want. The only way around this, as far as I can find, is not loading the CI object through:
Code:
$this->CI =& get_instance();
but doing it through
Code:
$CI =& get_instance();
It seems that I can do the latter only in the function itself (not in the constructor or anywhere else in the class. This means that I have to add this bit of code to every single function/method of my class.

Anyone can help me with a better solution. Your help would be much appreciated.
Thanks.


Problem class objects and CI object - El Forum - 08-12-2008

[eluser]wiredesignz[/eluser]
$CI =& get_instance() only returns a reference to CI, it does not make a copy of the super object (controller).


Problem class objects and CI object - El Forum - 08-13-2008

[eluser]bramb79[/eluser]
[quote author="wiredesignz" date="1218593779"]$CI =& get_instance() only returns a reference to CI, it does not make a copy of the super object (controller).[/quote]

Thanks for the info. Deep down I knew that already (had read it before). Smile

But that still leaves me with my question. What I would ideally want (or at least I think I do) is to make $CI (after stating $CI =& get_instance()) available in my entire class. Stating it in the constructor does not help. I think it is out of scope for other methods in the class. Putting $this->CI & get_instance() in the constructor does bring it within scope for the other methods but when I then create a new instance of the class I end up with the CI object as part of the data of that object.