Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to know whether a class can be traversed by foreach in PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "in PHP how to know whether a class can be traversed by foreach", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "in PHP how to know whether a class can be traversed by foreach" bar!

Detect whether a class can be traversed by foreach in PHP

In PHP, we can easily determine what type a variable is, or we can easily determine the length of an array to determine whether the array can be traversed. What about the class? How do we know if this class can be traversed through foreach? In fact, PHP has already provided us with a ready-made interface.

Class Obj1

{

Public $v = 'VRU Obj1'

Private $prv = 'prv:Obj1'

}

$obj1 = new Obj1 ()

Echo $obj1 instanceof Traversable? 'yes': 'no', PHP_EOL; / / no

Class Obj2 implements IteratorAggregate

{

Public $v = 'VRU Obj2'

Private $prv = 'prv:Obj2'

Public function getIterator ()

{

Return new ArrayIterator ([

'v' = > $this- > v

'prv' = > $this- > prv

])

}

}

$obj2 = new Obj2 ()

Echo $obj2 instanceof Traversable? 'yes': 'no', PHP_EOL; / / yes

As you can see from the above example, the first\ $obj1 cannot be judged by Traversable, so it cannot be traversed. The second $obj2 implements the iterator interface, which can be judged by Traversable. In the PHP manual, the Traversable interface is used to detect whether a class can be traversed by foreach.

This interface has several features:

Built-in classes that implement this interface can traverse using foreach without implementing the IteratorAggregate or Iterator interface. This is an internal engine interface that cannot be implemented in PHP scripts. The IteratorAggregate or Iterator interface can be used to replace it.

In other words, we do not need to implement this interface manually, only our class implements the interface related to the iterator to pass the verification of this interface. If we implement this interface alone, we will report an error and remind us that we should implement the IteratorAggregate or Iterator interface.

/ / Fatal error: Class ImplTraversable must implement interface Traversable as part of either Iterator or IteratorAggregate in Unknown

Class ImplTraversable implements Traversable {

}

In fact, in the previous article, we have verified that objects can be traversed, and there is no need to implement any iterator interface to be traversed by foreach. It outputs all the properties of the public.

/ / foreach

Foreach ($obj1 as $o1) {

Echo $o1, PHP_EOL

}

Foreach ($obj2 as $O2) {

Echo $O2, PHP_EOL

}

/ / V:Obj1

/ / V:Obj2

/ / prv:Obj2

In other words, the role of this Traversable interface is not obvious in practical use. It is believed that most of us have never used this interface to determine whether a class can be traversed. But as we can see from the above example, iterators can customize what we need to output. It is relatively more flexible and controllable than direct object traversal. In addition, in the case of an array forcing objects, the Traversable interface is also unable to determine.

$arr = [1,2,3,4]

$obj3 = (object) $arr

Echo $obj3 instanceof Traversable? 'yes': 'no', PHP_EOL; / / no

Foreach ($obj3 as $o3) {

Echo $o3, PHP_EOL

}

Thank you for your reading, the above is the content of "how to know whether a class can be traversed by foreach in PHP". After the study of this article, I believe you have a deeper understanding of how to know whether a class can be traversed by foreach in PHP. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report