Jump to content

Check for Specific Class


Go to solution Solved by Strider64,

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/278202-check-for-specific-class/
Share on other sites

  • Solution

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 by Strider64

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.