Manixat Posted March 10, 2013 Share Posted March 10, 2013 Hello freaks. I've been trying to familiarize with OOP for some time now and I've come to the point where I actually start building stuff. So now I'm trying to figure out what is the proper way of detecting an admin user. I have, of course, my user class and my admin class, which extends ( or I believe inherits is the correct term ) the user class. So if there's some content on a certain page that only admins are supposed to see, how do I detect if the user is an admin properly? What I can easily do is have an if statement checking the user on the db and depending on the outcome either load the user class or the admin class but that doesn't quite seem right to me, trying to cut down procedural code to the minimum and all.. So what is the proper way of doing this? Quote Link to comment https://forums.phpfreaks.com/topic/275450-oo-admin-stuff/ Share on other sites More sharing options...
Solution Jessica Posted March 10, 2013 Solution Share Posted March 10, 2013 You should probably just have admin be a property of your user class, not a whole new class. But if you want it that way you can use instanceOf, or have a function isAdmin() return true in admin and false in user Quote Link to comment https://forums.phpfreaks.com/topic/275450-oo-admin-stuff/#findComment-1417774 Share on other sites More sharing options...
Manixat Posted March 10, 2013 Author Share Posted March 10, 2013 You should probably just have admin be a property of your user class You know what they say about genius and simplicity .. Quote Link to comment https://forums.phpfreaks.com/topic/275450-oo-admin-stuff/#findComment-1417776 Share on other sites More sharing options...
trq Posted March 10, 2013 Share Posted March 10, 2013 I would agree with Jessica in this instance, but I would also like to point out a "proper way" for doing similar things. You need what is called a "factory". This factory would be responsible for creating different kinds of "User" objects dependant on a certain criteria. Without going into to much detail, the end call might look something like: $user = UserFactory::get(100); Where 100 is the user id as stored in the database. The user factory is then responsible for creating whatever "User" type needs to be created and returning it. Quote Link to comment https://forums.phpfreaks.com/topic/275450-oo-admin-stuff/#findComment-1417780 Share on other sites More sharing options...
Hall of Famer Posted March 10, 2013 Share Posted March 10, 2013 I think it depends on whether you want several additional fields to be stored for admin users, which are completely irrelevant for regular members. If this is true, you may subclass the user/member class with additional properties being neatly mapped to database fields. If it is just a status however, you are better off simply defining an additional property in your user/member class to label the specific user as an admin user. It totally depends on the application you have. And you may want to consider Data Mapper enterprise pattern to do the trick of fetching a user/admin object with a supplied criteria. These are good examples, I believe its quite flexible this way. You may extend its functionality to handle multiple criteria, say both a username and a password. The mapper can be extended to handle more than just single selection statement, you can read Matt Zandstra's book for more reference. $user = $userMapper->findByID(1); $user2 = $userMapper->findByUsername("Admin"); $user3 = $userMapper->findByEmail("youremail@gmail.com"); $user4 = $userMapper->findByIP("127.0.0.1"); Quote Link to comment https://forums.phpfreaks.com/topic/275450-oo-admin-stuff/#findComment-1417799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.