Access properties without knowing name

6

Is it possible within the class to access all your (properties) definitions without having their names?

For example, if I create an instance of a class $classe = new Classe() , and I'm defining things in it:

$classe->ComprarFile = true;
$classe->comidaDoCachorro = false;
$classe->valorDoDolar = '50 reais';

Would it be possible to access these properties in the class without knowing their names?

    
asked by anonymous 13.10.2015 / 03:18

1 answer

6

Tem. There are basically two options. One is using get_object_vars :

var_dump(get_object_vars($classe));

And the other is iterating over the object :

foreach ($classe as $key => $value) {
    print "$key => $value\n";
}

There you can use creativity to access in different ways using these techniques. The important thing is that the classes are associative arrays . Then you have easy names and values of all members.

    
13.10.2015 / 03:24