Check if variable exists inside class

I have the following PHP class:
 
class Class1
{
     public $var1;
     public $var2;
     public $var3;
}

Open in new window


How can I check if specific variable name is existed in Class1 or not?
LVL 12
Mohamed AbowardaSoftware EngineerAsked:
Who is Participating?
 
Chris StanyonConnect With a Mentor Commented:
You can use this to check if a specific variable exists. It will only return true for publicly visible vars.


echo (property_exists('Class1', 'var1')) ? "true\n" : "false\n";

Open in new window

0
 
Ray PaseurConnect With a Mentor Commented:
You might be able to use this function:
http://us.php.net/manual/en/function.get-defined-vars.php
0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
I see that the function get_defined_vars() return array of all the defined variables, I only need to get the variables I defined in the class "$var1, $var2, and $var3".

How can I do that?
0
 
Greg AlexanderConnect With a Mentor Lead DeveloperCommented:
Here
echo "<pre>";
print_r(get_class_vars('Class1'));
echo "</pre>";

Open in new window

0
 
Mohamed AbowardaSoftware EngineerAuthor Commented:
Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.