How to cache code in PHP?

Posted by Janis Peisenieks on Stack Overflow See other posts from Stack Overflow or by Janis Peisenieks
Published on 2011-03-06T15:04:17Z Indexed on 2011/03/06 16:10 UTC
Read the original article Hit count: 201

Filed under:
|
|

I am creating a custom form building system, which includes various tokens. These tokens are found using Regular Expressions, and depending on the type of toke, parsed. Some require simple replacement, some require cycles, and so forth.

Now I know, that RegExp is quite resource and time consuming, so I would like to be able to parse the code for the form once, creating a php code, and then save the PHP code, for next uses. How would I go about doing this?

So far I have only seen output caching. Is there a way to cache commands like echo and cycles like foreach()?


Because of misunderstandings, I'll create an example.

Unparsed template data:

Thank You for Your interest, [*Title*] [*Firstname*] [*Lastname*]. Here are the details of Your order!
[*KeyValuePairs*]
Here is the link to Your request: [*LinkToRequest*].

Parsed template:

"Thank You for Your interest, <?php echo $data->title;?> <?php echo $data->firstname;?> <?php echo $data->lastname;?>. Here are the details of Your order!
 <?php foreach($data->values as $key=>$value){
echo $key."-".$value
}?>
  Here is the link to Your request: <?php echo $data->linkToRequest;?>.

I would then save the parsed template, and instead of parsing the template every time, just pass the $data variable to the already parsed one, which would generate an output.

© Stack Overflow or respective owner

Related posts about php

Related posts about parsing