Sign in to follow this  

[web] php and $this

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been developing a login system for a website, with a class that represents a logged in user. One type of account, administrator, should have more functionality than other users. The way I originally did this was to have the functions specific to administrators built into the user class, but each time one of these functions was called, it checked to make sure that the user was an administrator. I've finally decided that this is not good design, and I'm trying to come up with a better way to do it. One method I've come up with is to have an administrator class that extends the user class, but the problem with this is that I don't know which type of class to create until after the user has logged in. I'd like the class to take care of itself, so the page using it shouldn't have to check and see if it's an administrator, then reassign the class type. Can I use $this to assign itself to a different type of class? This really doesn't seem like good design either, even if it would work, so any other ideas for how to best design this would be greatly appreciated. I'm building this under a three-tier architecture, so I could have the middle tier check and reassign the class, but this still doesn't seem like a good idea. Thanks again, I really appreciate your help. Edit: After thinking about this for a while, the idea that $this could reassign itself doesn't make any sense, and I don't know how any language could pull it off. How about, if a user is an administrator, a new object is created as a variable in the user class that holds administrator functionality? This still doesn't seem ideal; extending the user class to create a more functional administrator class just seems like the most logical solution. I'm still not sure about the best way to do this, though. If anyone experienced in design could give me their thoughts, I'd appreciate it a lot. [Edited by - sofsenint on July 18, 2005 8:42:49 PM]

Share this post


Link to post
Share on other sites
What kind of functionality does the administrator class have that the user class doesnt? Is it possible for you to have the page check to see if the user is an administrator before allowing the user to perform a certain action?

In my authentication system I have one user class, User, which has a method GetType() which returns the type of the user, Guest, Normal, Admin. If there is a function that requires certain access then that function first checks to see if the user has that access. For example


$user = GetCurrentUser();

// Delete something from the database
if ($user->IsLoggedIn() && $user->GetType() == 'Admin')
$db->DeleteSomething();
else
print 'You do not have permission to delete something';



This method works for me but maybe the functionality you need is best placed into an extended form of your user class?

Share this post


Link to post
Share on other sites
That would work. I was just concerned that leaving it up to the page itself to control access would be considered bad design. If that's not the case, I'll probably just do it that way. Thanks for your help!

Share this post


Link to post
Share on other sites
Just to clear up the $this stuff; $this can be reassigned within the class itself in PHP4, although it isn't generally done because it is considered bad design.

In PHP5 this behaviour is no longer supported and will throw a fatal error if you try to reassign $this.

Share this post


Link to post
Share on other sites

This topic is 4526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this