PHP takes uber long, fatal error

Status
Not open for further replies.

timtamboy63

Active Member
885
2009
0
0
Heres my error:
Fatal error: Maximum execution time of 60 seconds exceeded in C:\WAMP\www\Template\inc\templateClass.php on line 24

I'm assuming its because my php script takes too long, heres the relevant bit:

PHP:
    function parseIncludes(){
        $go = 1;
        while($go == 1){
            $result = preg_match('/{[^}]+}/', $this->template, $matches);
            if(isset($matches[0])){
                if($matches[0]){
                    $namear = explode('{', $matches[0]);
                    $name = substr_replace($namear[1] ,"",-1);
                    $file = 'templates/' . $name . '.tpl';
                    $includefile = file_get_contents($file);
                    $this->template = str_replace('{' . $namear[1], $includefile, $this->template);
                   
                }
                else{
                    $go = 0;
                }
            }
        }
    }
Any ideas?
 
9 comments
your checking for $matches 2 times, and the inner one is the one that sets $go to 0,

PHP:
function parseIncludes(){
        $go = true;
        while($go)
        {
            $result = preg_match('/{[^}]+}/', $this->template, $matches);
            if(isset($matches[0])){
               $namear = explode('{', $matches[0]);
               $name = substr_replace($namear[1] ,"",-1);
               $file = 'templates/' . $name . '.tpl';
               $includefile = file_get_contents($file);
               $this->template = str_replace('{' . $namear[1], $includefile, $this->template);
            }else
            {
                $go = false;
            }
        }
    }

its like

PHP:
while(true)
{
    if(false)
    {
        if(true)
        {
           //Code
        }
        else //if the first if is false, this if/else is never run.
        {
            break 2;
        } 
    }
}
 
You should also be aware of the fact parsing your tags with regex's will make the scrip very resource intensive. Especially if you don't implement your own caching system. You might wanna build a small state machine and parse it per character.
 
Caching is pretty simple to be honest

After your compiler has finished compiling the source, you need to store the the contents in a cache directory with a name that can easily be read such as index_php_43hdj8347ytjd837yt.cache

then before your compiler runs for lets say index.php

you can just check to see if its cached and load it, with some other checks like clear files that are 1 hour old, so when the compiler tries again, it wont be cached so continue to compile and then cache again.

PHP:
$Template = new Tempalte('index');

if($Template->isCatched())
{
    $contents = $Template->GetCachedVersion();
}else
{
     $Template->assign("whatever","some_value");
     //Template
     $contents = $Template->CompileAndCache();
}

$Template->FlushCatch(); //Delete files older than 3600s / 1 hour

echo $contents;

You get the idea
 
Status
Not open for further replies.
Back
Top