能不能给讲讲Segmentfault 用的框架里的Data模式是怎么把数组转换成对象的?

Viewed 4338

如:

// 从model中取出数据
$questions = $this->questionModel->listQuestions();
// 将数组放入迭代器
$this->questions = new Data_Question($questions);
// 展现模板
$this->view->render('questions.php');

到View中我们就可以操作Data_Question对象了

<?php foreach($questions as $q): ?>
<h2><a href="<?php $q->url(); ?>"><?php $q->title(); ?></a></h2>
<p><?php $q->summary(100, '...'); ?></p>
<?php endforeach; ?>

我想知道这个Data_Question是如何实现的

2 Answers

Class XXX implements Iterator, ArrayAccess, Countable{}

貌似重写一下ArrayObject就可以实现,谢谢啦