当前位置:Gxlcms > PHP教程 > 详解PHP如何遍历对象

详解PHP如何遍历对象

时间:2021-07-01 10:21:17 帮助过:2人阅读

对于php来说,foreach是非常方便好用的一个语法,几乎对于每一个PHPer它都是日常接触最多的请求之一。那么对象是否能通过foreach来遍历呢?

答案是肯定的,但是有个条件,那就是对象的遍历只能获得它的公共属性。

  1. // 普通遍历
  2. class A
  3. {
  4. public $a1 = '1';
  5. public $a2 = '2';
  6. public $a3 = '3';
  7. private $a4 = '4';
  8. protected $a5 = '5';
  9. public $a6 = '6';
  10. public function test()
  11. {
  12. echo 'test';
  13. }
  14. }
  15. $a = new A();
  16. foreach ($a as $k => $v) {
  17. echo $k, '===', $v, PHP_EOL;
  18. }
  19. // a1===1
  20. // a2===2
  21. // a3===3
  22. // a6===6

不管是方法还是受保护或者私有的变量,都无法遍历出来。只有公共的属性才能被遍历出来。其实,我们之前在讲设计模式时讲过的迭代器模式就是专门用来进行对象遍历的,而且PHP已经为我们准备好了相关的接口,我们只需要去实现这个接口就可以完成迭代器模式的创建了。具体的内容可以参考之前的设计模式系列文章:PHP设计模式之迭代器模式(推荐:《PHP视频教程》)

  1. // 实现迭代器接口
  2. class B implements Iterator
  3. {
  4. private $var = [];
  5. public function __construct($array)
  6. {
  7. if (is_array($array)) {
  8. $this->var = $array;
  9. }
  10. }
  11. public function rewind()
  12. {
  13. echo "rewinding\n";
  14. reset($this->var);
  15. }
  16. public function current()
  17. {
  18. $var = current($this->var);
  19. echo "current: $var\n";
  20. return $var;
  21. }
  22. public function key()
  23. {
  24. $var = key($this->var);
  25. echo "key: $var\n";
  26. return $var;
  27. }
  28. public function next()
  29. {
  30. $var = next($this->var);
  31. echo "next: $var\n";
  32. return $var;
  33. }
  34. public function valid()
  35. {
  36. $var = $this->current() !== false;
  37. echo "valid: {$var}\n";
  38. return $var;
  39. }
  40. }
  41. $b = new B([1, 2, 3, 4]);
  42. foreach ($b as $k => $v) {
  43. echo $k, '===', $v, PHP_EOL;
  44. }
  45. // rewinding
  46. // current: 1
  47. // valid: 1
  48. // current: 1
  49. // key: 0
  50. // 0===1
  51. // next: 2
  52. // current: 2
  53. // valid: 1
  54. // current: 2
  55. // key: 1
  56. // 1===2
  57. // next: 3
  58. // current: 3
  59. // valid: 1
  60. // current: 3
  61. // key: 2
  62. // 2===3
  63. // next: 4
  64. // current: 4
  65. // valid: 1
  66. // current: 4
  67. // key: 3
  68. // 3===4
  69. // next:
  70. // current:
  71. // valid:

假如今天的文章只是讲之前讲过的迭代器模式,那就太没意思了,所以,咱们还要来学习一个更有意思的应用。那就是让对象可以像数组一样进行操作。这个其实也是使用PHP早已为我们准备好的一个接口:ArrayAccess。

  1. // 让类可以像数组一样操作
  2. class C implements ArrayAccess, IteratorAggregate
  3. {
  4. private $container = [];
  5. public function __construct()
  6. {
  7. $this->container = [
  8. "one" => 1,
  9. "two" => 2,
  10. "three" => 3,
  11. ];
  12. }
  13. public function offsetSet($offset, $value)
  14. {
  15. if (is_null($offset)) {
  16. $this->container[] = $value;
  17. } else {
  18. $this->container[$offset] = $value;
  19. }
  20. }
  21. public function offsetExists($offset)
  22. {
  23. return isset($this->container[$offset]);
  24. }
  25. public function offsetUnset($offset)
  26. {
  27. unset($this->container[$offset]);
  28. }
  29. public function offsetGet($offset)
  30. {
  31. return isset($this->container[$offset]) ? $this->container[$offset] : null;
  32. }
  33. public function getIterator() {
  34. return new B($this->container);
  35. }
  36. }
  37. $c = new C();
  38. var_dump($c);
  39. $c['four'] = 4;
  40. var_dump($c);
  41. $c[] = 5;
  42. $c[] = 6;
  43. var_dump($c);
  44. foreach($c as $k=>$v){
  45. echo $k, '===', $v, PHP_EOL;
  46. }
  47. // rewinding
  48. // current: 1
  49. // valid: 1
  50. // current: 1
  51. // key: one
  52. // one===1
  53. // next: 2
  54. // current: 2
  55. // valid: 1
  56. // current: 2
  57. // key: two
  58. // two===2
  59. // next: 3
  60. // current: 3
  61. // valid: 1
  62. // current: 3
  63. // key: three
  64. // three===3
  65. // next: 4
  66. // current: 4
  67. // valid: 1
  68. // current: 4
  69. // key: four
  70. // four===4
  71. // next: 5
  72. // current: 5
  73. // valid: 1
  74. // current: 5
  75. // key: 0
  76. // 0===5
  77. // next: 6
  78. // current: 6
  79. // valid: 1
  80. // current: 6
  81. // key: 1
  82. // 1===6
  83. // next:
  84. // current:
  85. // valid:

这个接口需要我们实现四个方法:

  • offsetSet($offset, $value),根据偏移量设置值
  • offsetExists($offset),根据偏移量确定是否存在内容
  • offsetUnset($offset),根据偏移量删除内容
  • offsetGet($offset),根据依稀量获取内容

这里的偏移量就是我们常说的下标。通过实现这四个方法,我们就可以像操作数组一样的操作对象。当然,日常开发中我们可能并不会很经常的使用包括迭代器在内的这些对象遍历的能力。通常我们会直接去将对象转换成数组 (array) obj 来进行下一步的操作。不过,在java中,特别是JavaBean中会经常在类的内部有一个 List<T> 为自己的对象来表示自身的集合状态。通过对比,我们发现PHP也完全可以实现这样的能力,而且使用迭代器和 ArrayAccess 接口还能够更方便的实现类似的能力。这是非常有用的一种知识扩展,或许下一个项目中你就能运用上这些能力哦!

  1. 测试代码:
  2. https://github.com/zhangyue0503/dev-blog/blob/master/php/201912/source/PHP%E6%80%8E%E4%B9%88%E9%81%8D%E5%8E%86%E5%AF%B9%E8%B1%A1%EF%BC%9F.ph

以上就是详解PHP如何遍历对象的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行