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?
How to check for if a variable in a class exists?
Moderator: General Moderators
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
http://www.php.net/manual/en/function.g ... s-vars.php