We also fetch the comments list in the
template code. The whole code is too hazy to read and manage and may crash
successively at any core changes. But just think, if we turn the comments into a
collection of comment object for that post and all the posts into a collection of post
object for easier accessing, it will remove the burden of template designing as well as
create manageable code.
Let us implement Iterator pattern for our comments and posts and see how effectively
it turns your code into a readable piece of poem. After all, coding is poetry.
To use iteration effectively in PHP5 we can use Iterator interface. The interface is
shown below:
interface Iterator
{
function rewind();
function current();
function key();
function next();
function valid();
}
?>
The rewind() function of Iterator sets the index to the start of collection. The
Current() returns the current object. key() function returns the current key. The
Function next() returns if there are more object ahead in the current loop counter.
If the return is yes, this function returns true, otherwise it returns false. The valid()
function returns the current object if it has any value in it.
Pages:
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102