The YII framework can use foreach to iterate over objects and why objects can be accessed directly using an array form
During the use of the YII framework, we can use foreach to iterate through the properties of the object returned by the FindAll method, etc.
Why is it? In fact, this is related to the interface implemented by Cmodel, and then we look at the whole process of its implementation.
For a model that we define, it inherits the virtual class Cactiverecord,cactiverecord class that inherits from Cmodel, as follows:
Class Special extends Cactiverecord {
}
Abstract class Cactiverecord extends cmodel{
}
The most critical place is the Cmodel class implements the Iteratoraggregate interface.
The Getiterator method implemented in the Cmodel class returns all the properties of the model, using an iterator that is implemented by the YII framework Cmapiterator, and Cmapiterator implements the iterator interface
The same truth,
Because Cmodel implements the Arrayaccess interface, it can be accessed directly as an array
Turn from
Http://blog.sina.com.cn/s/blog_60a5a7e00100uyat.html
The YII framework can use foreach to iterate over objects and why objects can be accessed directly using an array form