I need some thoughts on code duplication and separation
| From: | Rico Secada | Date: | Thu, 21 Oct 2010 02:05:50 +0000 |
| Subject: | I need some thoughts on code duplication and separation | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-308908@lists.php.net to get a copy of this message | ||
Hi.
I am working on a small system where I am both trying to avoid code
duplication and at the same time I am trying to keep the presentation
logic separated from the application logic.
I am using sessions and are avoiding "headers already sent" problem by
keeping the HTML out of the application.
For example, I would like to have a common header.php file, but it is
difficult to create this since one file needs to have some specific
Javascript located in the <head> </head> tags, but the other files
doesn't need this.
Another file needs to have a specific "onload" call in the <body> tag,
while yet another file also needs to have an "onload" call, but with
different attributes.
I have been looking around in other systems to see what kinds of
solutions are being used - as inspiration.
I have been thinking about the following solutions:
1. Create only ONE header.php file that contains a lot of conditionals
depending on what file is including it - the output of HTML/Javascript
changes.
I believe this would turn into a very ugly hack. Difficult to maintain.
2. Create a HTML generating class with a set of methods that each
contains an adequate amount of parameters. Each method maintains its
own HTML tag. For example, docType($type) would generate the doctype
specification.
I believe this is a "cleaner" solution, but the problem with code
duplication isn't avoided.
Some of the presentation logic contains conditionals and the HTML
changes when the conditional changes, hence the header content changes,
but the <doctype>, <html>, and <head> doesn't necessarily change and
they would get duplicated a couple of times in some files.
3. Avoid the problem all together, use output buffering and completely
forget about separation between application and presentation.
I hope I make sense.
Any thoughts on these kinds of problems?
Best regards.