循环类属性

时间:2011-11-08 10:18:47

标签: php oop

如何循环Class

的所有属性
class object {
    public $a = 1;
    public $b = 2;
    public $c = 3;
}

,输出将是

"property $a is 1";
"property $b is 2";
"property $c is 3";

2 个答案:

答案 0 :(得分:5)

你可以use a foreach loop

foreach (new object as $prop => $value) {
    echo "property \$$prop is $value\n";
}

答案 1 :(得分:2)

您可以查看get_object_vars(),但在查看Polynomial的评论后不会看。