作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
如何遍历 php 类的(公共(public)或私有(private))属性?
最佳答案
tl;dr
// iterate public vars of class instance $class
foreach (get_object_vars($class) as $prop_name => $prop_value) {
echo "$prop_name: $prop_value\n";
}
进一步的例子:
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();
输出:
array(2) {
["b"]=> int(1)
["c"]=> NULL
}
array(4) {
["a"]=> NULL
["b"]=> int(1)
["c"]=> NULL
["d"]=> NULL
}
关于php - 遍历 php 类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/861254/
我是一名优秀的程序员,十分优秀!