Welcome Guest, Not a member yet? Register   Sign In
Including class dependencies
#1

[eluser]TheFuzzy0ne[/eluser]
Hi everyone.

I'd like to create a library called "user", which will essentially be used to represent the current user in any given request, as well as logging them in and out. It will have a setter and getter that will set variables in the session class' userdata array. The class will be automatically initialized when it starts. It will check for a user id in the cookie on each request when it's instantiated, and it will refresh any existing session data with that from the database, so for example, if a user is banned from the site, the ban will take effect immediately, and log them out (hopefully this makes sense). Basically, it's a lot like an auth system.

It needs to make use of my user model and the session library, I'm just wondering what the best way might be to go about ensuring that the session library and my user model have been loaded first. Should I just load them both in the constructor? It seems like it might be the only option, but I just want to check I'm not missing something.

Thanks in advance. Comments welcome. Smile
#2

[eluser]Dam1an[/eluser]
Thats the route most libraries take
Also, if the session/user model has already been loaded, it won;t get loaded again so no problems
#3

[eluser]TheFuzzy0ne[/eluser]
I understand that PHP 4 has problems with loading libraries/models in the constructor. Would it be breaking the whole MVC thing if put code outside of the class to check they are loaded, or load them if they aren't?

Code:
var $codeigniter =& get_instance();

if ( ! is_property($codeigniter, 'session')
{
    $codeigniter->load->library('session');
}

if ( ! is_property($codeigniter, 'user_model')
{
    $codeigniter->load->model('user_model');
}

class User
{
    # ...
#4

[eluser]Dam1an[/eluser]
Ah yes, the pit falls of PHP4 lol
I'm not 100% sure if that will work... I have a feeling when you do $user = new User() is might just parse the contents of the class, not the whole file
I know the CI helpers get an instance at the start of each method instead of doing what you're doing at the stage of the file (or am I thinking of something else)

Try it and let us know if it works
#5

[eluser]TheFuzzy0ne[/eluser]
I think they only use get_instance() in each function is to keep variables out of the global scope. Come to think about it, perhaps that's a good enough reason not to do it the way I'm proposing. In theory, my method will be safe, regardless of whether the file is being loaded using include/require or if it's loaded via CodeIgniter's loader. The class in this case is intended to be loaded using CodeIgniter's loader, so I can do things like:

Code:
$this->user->do_login($username, $password);
$this->user->is_logged_in(); # and so on.

I'll check it out, I just wanted to see if I was breaking any rules, or missing something blatantly obvious. Of course, the same checks and some extra code to import a reference to the other model and library can be done in the constructor.
#6

[eluser]Dam1an[/eluser]
When you use the CI loader, does it not just end up doing an include_once and and creating a new object in the scope of the CI super object?
#7

[eluser]TheFuzzy0ne[/eluser]
Yes, it does exactly that, but I'm not sure what you're getting at.
#8

[eluser]Dam1an[/eluser]
Don't worry, I know, and just realised its irrelevant anyway
#9

[eluser]Colin Williams[/eluser]
If you are so concerned about adhering to MVC, then you should just have a User model. Not sure why you want to go this route and have it "make use of your user model." Just make it part of the User model.
#10

[eluser]TheFuzzy0ne[/eluser]
Colin, I'm not sure I understand what you mean. The user library only makes a single call to user_model to get the data for that user. All other methods are simply methods for convenience, or setting/retrieving values in the $this->session->userdata array. If you could explain in a bit more detail, what you mean by " Just make it part of the User model", I'd really appreciate it.

Thanks.

EDIT: By MVC, I didn't mean strict MVC, but more the relaxed approach to MVC that CodeIgniter offers, i.e the use of libraries and helpers. Hopefully that makes sense.




Theme © iAndrew 2016 - Forum software by © MyBB