Check if class has field by line

I would do it Google, but honestly I have no idea what to look for

Say I have this class

class a
{
    public $a_a, $a_b, $a_c;
}

$true = "a_a";
$false = "a_e";

How to use strings to prove that a class contains a_a but not a_e?

thank

+3
source share
2 answers

WITH

In your case:

var_dump( property_exists('a', 'a_a') ); // TRUE

You can also use the Reflection API , but this excess for this UseCase:

$reflector = new ReflectionClass('a');
var_dump( $reflector->hasProperty('a_e') ); // FALSE
+7
source

property_exists Reflection. , , PHP 5.3 property_exists . , PHP 5.2 , Reflection.

+1

Source: https://habr.com/ru/post/1765649/


All Articles