iterate over properties of a php class iterate over properties of a php class php php

iterate over properties of a php class


tl;dr

// iterate public vars of class instance $classforeach (get_object_vars($class) as $prop_name => $prop_value) {   echo "$prop_name: $prop_value\n";}

Further Example:

http://php.net/get_object_vars

Gets the accessible non-static properties of the given object according to scope.

class foo {    private $a;    public $b = 1;    public $c;    private $d;    static $e; // statics never returned    public function test() {        var_dump(get_object_vars($this)); // private's will show    }}$test = new foo;var_dump(get_object_vars($test)); // private's won't show$test->test();

Output:

array(2) {  ["b"]=> int(1)  ["c"]=> NULL}array(4) {  ["a"]=> NULL  ["b"]=> int(1)  ["c"]=> NULL  ["d"]=> NULL}