The YII framework can use foreach to iterate over objects and why objects can be accessed directly using an array form

Source: Internet
Author: User
Tags yii

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.