ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 对象遍历 使用 `foreach` 语句遍历对象,默认情况下,所有可见属性都将被用于遍历。 ``` <?php class MyClass { public $var1 = 'value 1'; public $var2 = 'value 2'; public $var3 = 'value 3'; protected $protected = 'protected var'; private $private = 'private var'; function iterateVisible() { foreach($this as $key => $value) { echo "{$key} => {$value}" . PHP_EOL; } } } $obj = new MyClass(); /* ---------- 外部遍历 ------------ */ foreach($obj as $key => $value) { echo "{$key} => {$value}" . PHP_EOL; } echo "------------------" . PHP_EOL; /* ---------- 内部遍历 ------------ */ $obj->iterateVisible(); ?> ```