Let's assume that we have a PHP class Page, which generates PHP Page objects in a CMS. Inside the class, we have a function called GetPages(), which returns an array of Page objects for all pages in the system.
If we want to output these in a table, we could do something like:
foreach(Page::GetPages() as $page)
{
echo $page->title.'<br />';
}
This is quite a clean solution, but we're essentially performing two loops, when we only need to run it once. I came across this while working on a bespoke CMS that a client has had commissioned, and they now want us to overhaul it (simply because of speed).
I was thinking that it might be better to remove the GetPages() function from the class, and do something like this on admin interface:
$pages = "SELECT `id` FROM `ig_pages`";
$result = Database::Singleton()->Query($pages);
while($page = $result->fetch_object())
{
$this_page = new Page($page->id);
echo $this_page->title.'<br />';
}
Obviously from an architectural point of view, the function should really be included inside the Page class, but I have some concerns about the loops running twice effectively. Can anyone suggest a better approach for this?
EXPLAIN-ing to do there.$this_pageis aPageobject.