3
\$\begingroup\$

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)?

<?php

class OutputConstructor extends ContentRetriever // Or whatever I decide to name it
{

    protected $htmlHeadBlock;

    public function __construct()
    {

        $this->setHtmlHeadBlock();

    }

    protected function setHtmlHeadBlock()
    {

        ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <title><?php echo $this->pageTitle; ?></title>

        <link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
        <link rel="icon" href="images/favicon.ico" type="image/x-icon" />

        <meta name="description" content="<?php echo $this->pageDescription; ?>" />
        <meta name="keywords" content="<?php echo $this->pageKeywords; ?>" />

        <link type="text/css" rel="stylesheet" href="css/style.css" />
        <script type="text/javascript" src="js/functions.js"></script>
    </head>
<?php
        $this->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!

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

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:

  • 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

\$\endgroup\$
1
  • \$\begingroup\$ Great example - This really does give me some ideas for the project I'm working on. If it helps you make sense of my code sample, it (with some clear differences) is intended to be used similarly to the Head class in your code. \$\endgroup\$
    – 65Fbef05
    Apr 28, 2011 at 12:23
0
\$\begingroup\$

Well, I think the most convenient way is to use templates. Your HTML block with variables is actually a template. You can use some template engine like Twig or Smarty to build your template from the data you supply or you can use PHP extended syntax to do that like this:

<html>
   <body>
      <p<?php if ($highlight): ?> class="highlight"<?php endif;?>>This is a paragraph.</p>
   </body>
</html>
\$\endgroup\$
5
  • \$\begingroup\$ That's good to know, but doesn't really touch my question. There is a lot of literature on how to apply object oriented principles to PHP, but none that really spends any time on object oriented output management or integration. I want to know where in my general design scheme I should be piecing together and formatting my output, and how I should be setting/storing it before output. \$\endgroup\$
    – 65Fbef05
    Apr 27, 2011 at 20:24
  • \$\begingroup\$ @65Fbef05 I don't really understand you. I think you are mixing templating and MVC. HTML is just an HTML, it is not an object itself, it is data. Template engines are object-oriented mostly. And MVC is today's most popular way of structuring web applications. \$\endgroup\$ Apr 28, 2011 at 6:58
  • \$\begingroup\$ FractalizeR, please provide a source to your comment "MVC is today's most popular". \$\endgroup\$
    – RobertPitt
    Apr 28, 2011 at 9:35
  • \$\begingroup\$ @RobertPitt en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Since it became an architectural pattern, I believe one can say, that it's really popular. \$\endgroup\$ Apr 28, 2011 at 10:19
  • \$\begingroup\$ I'm not familiar enough with design patterns to name a common pattern that I'm going for, but it's not model-view-controller abstraction. I do intend to have those processes abstracted, but I think you're missing my point altogether. HTML, XML, JSON, plain text... etc; I just used HTML in my example but its use here is irrelevant. Whatever the actual output is, I'm trying to decide how structure my output in an object designed application. \$\endgroup\$
    – 65Fbef05
    Apr 28, 2011 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.