21

Every time I get a variable through $_GET I want to verify that it is indeed an object of the User class.

So:

if (isUser($_GET['valid_user']))
{
...
}

is there a built in function to use instead of this so called "isUser"?

Thanks a lot!

5 Answers 5

51

You can verify any variable for a certain class:

if ($my_var instanceof classname)

however, in your case that will never work, as $_GET["valid_user"] comes from the request and is never going to be an object.

isUser() is probably a custom function from a user management library that authenticates the current session. You need to take a look how that works if you want to replace it.

Sign up to request clarification or add additional context in comments.

1 Comment

For others: note that classname is not a string - it is the class identifier (that may be imported with the use keyword).
6

There is a function in the PHP language, which might help you:

bool is_a ( object $object , string $class_name )

From the documentation: Checks if the object is of this class or has this class as one of its parents

Comments

2

you could try this:

if (is_object($_GET['valid_user']))
{
     // more code here
}

Comments

0

is_object to check if it is a object and link below explains how to check which class

http://hr.php.net/manual/en/reflectionclass.isinstance.php

Comments

0

$_GET[] variables are more constants than variables, they are read only .. but happen to be accessed like you would any other $variable.

In your example:

if (isUser($_GET['valid_user']))
{
...
}

I would hope that it would read:

if (isUser($some_sanatized_variable))

.. unless isUser() is what's doing that. 'Variables' that PHP sets don't belong to any class, which illustrates my concern that isUser() may not adequately know what its dealing with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.