Marty
Marty

Reputation: 544

PHP call functions from different places

I need a little help. I have this class, that measures page load time

<?php 
class Page {
    private static $start;
    private static $end;

    static public function setStartPage(){

       return self::$start = microtime(true);
    }

    static public function setEndPage(){

      return  self::$end = microtime(true);
    }

    static public function loadTime(){
        return  self::$end -  self::$start;
    }
}
?>

and i call in start of header this:

<?php
Page::setStartPage();
?>

in html body i calling this

<?php echo Page::loadTime();?>

and in the end of page I calling this

<?php
Page::setEndPage();
?>

How do I send values from end page to body in loadTime function?

Upvotes: 2

Views: 36

Answers (1)

LihO
LihO

Reputation: 42083

If you read book page by page, how can you tell how the story ends in the middle of book?

Instead of:

  1. start timer
  2. print content
  3. stop timer
  4. try to change content that has already been printed

try:

  1. start timer
  2. build content, but don't print
  3. stop timer
  4. add measured time to content
  5. print enhanced content

Most of MVC frameworks out there using layouts do the same. You first prepare content of the page, and just at the end you process a layout, where rendered page is placed into it.

Upvotes: 1

Related Questions