berridgeab Posted May 20, 2013 Share Posted May 20, 2013 Hello Looking for a way to check for a specific class, can't believe I can't solve something so simple. <?php class UnregisteredUser {} class RegisteredUser extends UnregisteredUser {} $unregisteredUser = new UnregisteredUser(); $registeredUser = new RegisteredUser(); if($registeredUser instanceof UnregisteredUser) { //Section A } elseif($registeredUser instanceof RegisteredUser) { //Section B } ?> I want to check if variable $registeredUser is an instance of RegisteredUser. With the above code, Because RegisteredUser inherits from UnregisteredUser, it executes section A (even though I don't want it too). Is there anyway I can check if a class is actually a specific class with no consideration to its inherited properties? I know instanceof doesn't work, nor does is_a(). Is my class design ok? The only reason RegisteredUser inherits from UnregisteredUser is because it contains functions and properties that are common to both classes. I could seperate them but defeats the purpose of inheritance. Quote Link to comment https://forums.phpfreaks.com/topic/278202-check-for-specific-class/ Share on other sites More sharing options...
Muddy_Funster Posted May 20, 2013 Share Posted May 20, 2013 On the surface of it, that doesn't look like the right way to do things. I think you would be better having a single "User" class with, perhaps, a status property that defines if the user is registered or unregistered. Quote Link to comment https://forums.phpfreaks.com/topic/278202-check-for-specific-class/#findComment-1431156 Share on other sites More sharing options...
ignace Posted May 20, 2013 Share Posted May 20, 2013 Like Muddy already said you need a User class, the registered state is nothing more but to check if the user has an ID or not. Quote Link to comment https://forums.phpfreaks.com/topic/278202-check-for-specific-class/#findComment-1431158 Share on other sites More sharing options...
Solution Strider64 Posted May 20, 2013 Solution Share Posted May 20, 2013 (edited) I find it easier to find out what privileges a user has then doesn't have and having a User/Member class simplifies that for all one has to do is something like: // Method returns a Boolean if the user is an administrator: public function isAdmin() { return ($this->userType == 'admin'); } Edited May 20, 2013 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/278202-check-for-specific-class/#findComment-1431161 Share on other sites More sharing options...
berridgeab Posted May 20, 2013 Author Share Posted May 20, 2013 On the surface of it, that doesn't look like the right way to do things. I think you would be better having a single "User" class with, perhaps, a status property that defines if the user is registered or unregistered. I omitted the functions / properties to keep the example simple but I do have a basic User class. It just made sense to me that seen as a RegisteredUser has all the properties of an UnregisteredUser that the RegisteredUser should extend from that class. I could get around it doing the checks you have suggested but I can't believe there is nothing native in PHP that says "are you an instance of this particular class?". In the end I have opted for get_class($var) which allows me to check the class string name. Quote Link to comment https://forums.phpfreaks.com/topic/278202-check-for-specific-class/#findComment-1431162 Share on other sites More sharing options...
Jessica Posted May 20, 2013 Share Posted May 20, 2013 I omitted the functions / properties to keep the example simple but I do have a basic User class. It just made sense to me that seen as a RegisteredUser has all the properties of an UnregisteredUser that the RegisteredUser should extend from that class. The point being made is that you should not have 3 user classes. You need 1. You don't need RegisteredUser and UnregisteredUser. I could get around it doing the checks you have suggested but I can't believe there is nothing native in PHP that says "are you an instance of this particular class?". There is. Quote Link to comment https://forums.phpfreaks.com/topic/278202-check-for-specific-class/#findComment-1431164 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.