Forum Moderators: coopster

Message Too Old, No Replies

Best Way to Include Repetitive Code

Constants or Separate File

         

fx3000

8:11 pm on Nov 23, 2004 (gmt 0)

10+ Year Member



On a large site I work on, I use constants for labels that are used repeatedly, for example:

define('VAR1','Shipping and Ordering Information');

My question is, when you have short sections of HTML code (say like 7-15 lines) that are used repeatedly, for example:

<tr>
<th>Description</th>
<th align="center">Part Number</th>
<th>Quantity 1-9</th>
<th align="center">Qty 10 or more</th>
<th align="center">Shopping Cart</th>
</tr>

.... is it better store to it as a constant in one file with many constants OR in it's own separate include file? I would be including either method using PHP include. I guess what I am really saying is, what is the limit for what you want to hold in a constant?

jollymcfats

9:43 pm on Nov 23, 2004 (gmt 0)

10+ Year Member



fx3000,

Welcome to WebmasterWorld!

With a big file of verbose constants, PHP has to parse & load all of the text for every constant into memory regardless of whether the request uses zero, one or more of the constants. If you break them out into includes, then you can load them on demand, taking that hit only when you need to.

All that said, a file of big constants is probably not a very big deal, performance-wise. You're probably safe going with whichever one is easier to maintain.

I personally use functions to handle "text macro"-type insertions. Something like <?= qtyTableHeader()?>. In the function definition you're free to do an include, return literal text, or use whatever strategy works best for the performance you need. And if you change that strategy, you don't need to update it anywhere but the function definition. Everywhere that you're using <?= qtyTableHeader()?> will automatically pick up the benefits without any changes needed.

fx3000

4:50 pm on Nov 24, 2004 (gmt 0)

10+ Year Member



jollymcfats, thanks for your reply. I think functions will be the best way to go. I already have a file included on each page that has several commonly used functions, so I can just add some functions to it that are something like echo 'code goes here'. If I understand you correctly constants are always processed/parsed but functions are only called/loaded when they are needed.

bloke in a box

4:55 pm on Nov 24, 2004 (gmt 0)

10+ Year Member



If I understand you correctly constants are always processed/parsed but functions are only called/loaded when they are needed.

Correct, ensure you use include rather than require (providing you're using PHP).

I have a site of approx 5000 pages which run on a total of 5 functions. The entire sites code is probably around 70 lines. With a bit of caching the site can take one hell of a hammering :)

fx3000

5:20 pm on Nov 24, 2004 (gmt 0)

10+ Year Member



Thanks for your reply Bloke. So I guess another benefit of using functions is the include file with functions gets cached?

As for require (as opposed to include), I definitely wouldn't want to use it and get a fatal error that stops my page from loading.

Bloke, for your 5000 page site do you go dynamic and just use one template file or static with a bunch of includes?

bloke in a box

9:35 am on Nov 25, 2004 (gmt 0)

10+ Year Member



The site is all dynamic, it just uses 404 ErrorRedirect handling to pass www.widgets.com/latestwidgets/ (the 404'ed url) to a Case statement.

That just checks that 'latestwidgets' is actually a section on the site and serves up the correct page. If it doesn't exist then it serves up a 'proper' 404 page.