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

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

WBOY
WBOYOriginal
2016-06-06 20:51:12987browse

如:

// 从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%20%24q->url();%20?>"><?php $q->title(); ?></a></h2>
<p><?php $q->summary(100, '...'); ?></p>
<?php endforeach; ?>

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

回复内容:

如:

// 从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%20%24q->url();%20?>"><?php $q->title(); ?></a></h2>
<p><?php $q->summary(100, '...'); ?></p>
<?php endforeach; ?>

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

Class XXX implements Iterator, ArrayAccess, Countable{}

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn