patternphpMinor
PHP Output Block Defining and Best Practices
Viewed 0 times
phpblockoutputpracticesanddefiningbest
Problem
I've been rolling around several ideas or approaches in my head, and none seem any more useful or "right" than another. I'm sure this is extremely objective but I'm looking for a close-to-definitive answer on the object oriented approach to large block string storage and output in PHP. Given the following example, how could I change my approach in a way that might cause Zeev Suraski to pat me on the back and say Mazel Tov (beside using HTML5)?
The general theme I'm going with in this example class is that on construction, html is stored in a variable via output buffer and then returned by a public method. When evaluating this code, consider that the html will not be static. Page title, description, keywords and the such will be subbed out with dynamic property references. My overall question is: In object oriented programming, ideally where should the output be stored? PHP's
setHtmlHeadBlock();
}
protected function setHtmlHeadBlock()
{
ob_start();
?>
pageTitle; ?>
pageDescription; ?>" />
pageKeywords; ?>" />
htmlHeadBlock = ob_get_clean();
}
public function getHtmlHeadBlock()
{
return $this->htmlHeadBlock;
}
} // End of OutputConstructor
?>The general theme I'm going with in this example class is that on construction, html is stored in a variable via output buffer and then returned by a public method. When evaluating this code, consider that the html will not be static. Page title, description, keywords and the such will be subbed out with dynamic property references. My overall question is: In object oriented programming, ideally where should the output be stored? PHP's
DOMDocument class, however useful, doesn't really answer this question for me. Also in this instance, is there any best practice trade-off between using an output buffer and heredoc syntax? Thanks in advance for your guidance!Solution
The first problem i can see and many other's is that your hardcoding output data, why would you hard code output data on within a language that was built for dynamic content creation.
I would suggest that you restructure your class to combine several entites into one, for example, when creating a html web page, i usually is constructed of 5 major compoinants, for isntance:
Taking this into consideration i would like to make my code so it would act in the same way, create 6 or so classes called: HTMLPage, HTMLHead, HTMLBody, HTMLBodyHeader, HTMLBodyMain, HTMLBodyFooter
Now each class should be assigned to it's parent, for example:
The Head and Body belong to the Page, and the Header,Main and Fotter belong to the Body, this taken into consideration you should look at the following structure:
I will leave it down to you to figure out the classes
I would suggest that you restructure your class to combine several entites into one, for example, when creating a html web page, i usually is constructed of 5 major compoinants, for isntance:
- Page
- Head
- Body
- Header
- Main
- Footer
Taking this into consideration i would like to make my code so it would act in the same way, create 6 or so classes called: HTMLPage, HTMLHead, HTMLBody, HTMLBodyHeader, HTMLBodyMain, HTMLBodyFooter
Now each class should be assigned to it's parent, for example:
The Head and Body belong to the Page, and the Header,Main and Fotter belong to the Body, this taken into consideration you should look at the following structure:
/*
* Create a Page
*/
$Page = new Page("DOCTYPE");
/*
*Create a Head
*/
$Head = new Head();
$Head->title = "My Practical Home Page";
$Head->addScript("http://google.com/example.js");
$Head->addStyle("http://google.com/example.css");
$Head->addMeta('blah','..blah..');
/*
* Create a Body Object
*/
$Body = new Body("charset");
$Body->layout = new HTMLLayout("some_file_to_specifiy_the_layout");
$Body->SetContent('LeftBlock.Top','Hello World');
/*
* Create Footer
*/
$Footer = new Footer('default_footer.php');
/*
* Add all the segments to the main page!
*/
$Page->head = $Head;
$Page->body = $Body;
$Page->body->header = $Header;
$Page->body->main = "....";
$Page->body->footer= $Footer;
/*
* Send the page to the browser!
*/
$Page->Display();I will leave it down to you to figure out the classes
Code Snippets
/*
* Create a Page
*/
$Page = new Page("DOCTYPE");
/*
*Create a Head
*/
$Head = new Head();
$Head->title = "My Practical Home Page";
$Head->addScript("http://google.com/example.js");
$Head->addStyle("http://google.com/example.css");
$Head->addMeta('blah','..blah..');
/*
* Create a Body Object
*/
$Body = new Body("charset");
$Body->layout = new HTMLLayout("some_file_to_specifiy_the_layout");
$Body->SetContent('LeftBlock.Top','Hello World');
/*
* Create Footer
*/
$Footer = new Footer('default_footer.php');
/*
* Add all the segments to the main page!
*/
$Page->head = $Head;
$Page->body = $Body;
$Page->body->header = $Header;
$Page->body->main = "....";
$Page->body->footer= $Footer;
/*
* Send the page to the browser!
*/
$Page->Display();Context
StackExchange Code Review Q#2128, answer score: 3
Revisions (0)
No revisions yet.