[web] php and $this

Started by
3 comments, last by sofsenint 18 years, 9 months ago
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]
-----------------------------Weeks of programming can save you hours of planning.There are 10 kinds of people in this world-- those who understand binary and those who don't.
Advertisement
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 databaseif ($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?
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!
-----------------------------Weeks of programming can save you hours of planning.There are 10 kinds of people in this world-- those who understand binary and those who don't.
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.
Thanks for your reply! That's good to know for future reference.
-----------------------------Weeks of programming can save you hours of planning.There are 10 kinds of people in this world-- those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement