How to check for if a variable in a class exists?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dschast
Forum Newbie
Posts: 1
Joined: Wed Dec 18, 2002 2:51 pm

How to check for if a variable in a class exists?

Post by dschast »

I'm creating a class/structure that holds specific information like:

class Person
{
var firstname;
var lastname;
var title;
}

Is there any way to CHECK to see if a certain variable was declared in the class or not? I'm writing a function that goes through each element in $_POST and adds the value to the class object IF AND ONLY IF that variable was declared in the class object, but so far I haven't found a way to find out if the variable was declared or not. isset() only returns true if a value was set.

The only way I was able to get this to work is just to just declare one variable $data and make it an associative array. Then I used the array_key_exists() function. This workaround works but it seems a bit redundant that I have an associative array inside of a structure.

What do you think?
User avatar
nathus
Forum Commoner
Posts: 49
Joined: Thu Dec 12, 2002 6:23 pm

Post by nathus »

get_class_vars() might do what you want. Returns an associative array of the vars declared in a class.

http://www.php.net/manual/en/function.g ... s-vars.php
Post Reply