1

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?

9
  • 6
    If I was worried about speed - my first port of call would be the database and a bunch of EXPLAIN-ing to do there. Commented Aug 29, 2012 at 13:17
  • I don't think that running foreach to iterate over an array is going to cost you much (at all) in terms of speed. When overhauling an app for speed, your first focus should be the db. Foreach is very inexpensive computationally. Commented Aug 29, 2012 at 13:18
  • Don't just go in blind, but rather profile your application to see what the bottlenecks are. Just changing random things is the wrong approach. Commented Aug 29, 2012 at 13:18
  • Am I being dumb? I can't see where the second loop is....? Commented Aug 29, 2012 at 13:19
  • 1
    Because $this_page is a Page object. Commented Aug 29, 2012 at 13:25

3 Answers 3

2

When you assume, you make an ass out of u and me. (well, not me directly but you get it)

Profile your application with XDebug, analyze results, and then come back with a concrete problem regarding speed.

And remember, premature optimization is the root of all evil.

Sign up to request clarification or add additional context in comments.

Comments

0

You can create your output string while you are creating your array, but if you don't have infinitely many pages looping twice through the pages will not be too slow.

I think you should have a string member of your page which will contain your results and you should have a function which would generate the html which wraps your values. But I think this is not the main issue, you should optimize your database by creating indexes so your selects will be quicker.

Comments

0

Put a public var in top of your class then trigger your function to place that pages array in that var before returning then:

class myPagescreator(){

public var $pages = array();

function __construct(){

}

function GetPages(){
  //here your query to get all pages info from database (PDO fetchALL or param::fetchCOLUMN return a nice array from your queries)
  $this->pages = $DBqueryReturnInArray;
  #
  # here you should call and merge all the infos you need with a function call like $this->functionToGetPageBody($myPagesID); to merge to your array of pages ($this->page = array_merge($this->page,$thispageBody)) in a foreach or a while

  return $this->pages; 
  }
}

//The on your class call from your CMS
$pageMe = new Page();
//Activate the function in your class
$pageMe->GetPages();

// getting the var of arrayOfpages in the class Page
$imAnArrayOfPages = $pageMe->pages;
// Or getting the return of the function that will return your var at the same...
$imAnArrayOfPages = $pageMe->GetPages();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.