开发者

Would you consider this a stupid use of a class file?

I have this class, and I was wondering, do you consider it a waste? As in should I do this? I understand how class's work and what makes them good but im failing (in a website point of view) to understand what use they have.

<? 
class PageHandler {
    var $html;
    function header () {
        $this->html .= "<html>
            <head>
                <title>Nuclear Summer</title>
            </head>
        <body&开发者_如何学Pythongt;";
    }
    function footer () {
        $this->html .= "
            </body>
            </html>
            ";
    }
    function input ($html) {
        $this->html .= $html;
    }
    function output () {
        echo $this->html;
    }
}
?>


Yes.

This is a bad use of a class. What you're using a class for is what you should be using views for (if you're going for a MVC approach, which is a good way to go).

You create a main layout view, which contains code common to all pages (e.g. header, footer) and then you route incoming requests to an appropriate controller, which fills in the "middle" of the layout with appropriate content.

CodeIgniter is a great PHP framework for getting to grips with MVC. It even has documentation on it.


Your PageHandler is in terms of PHP frameworks something like a "view"-class. You can instantiate it for a page and provide it with more functionality. You are reusing the header and the footer, so that's generally speaking a godd idea.

But your example is to simple to say more about it.


this is example for php4 and not usual for php5, also this is not the best usage for classes to create output templates, try to explore Zend Framework it is good enough example of OOP and MVC in PHP http://framework.zend.com/manual/en/

Example of your code for php5 but still not the best usage for OOP

class PageHandler {

    private $html;

    private function header () {
        $this->html .= "<html>
            <head>
                <title>Nuclear Summer</title>
            </head>
        <body>";
    }
    private function footer () {
        $this->html .= "
            </body>
            </html>
            ";
    }
    public function input ($html) {
        $this->html .= $html;
    }
    public function output () {
        $this->html = $this->header() . $this->html . $this->footer();
       return $this->html;
    }
}

$page = new PageHandler();
$page->input('some content');
echo $page->output();


Using a class as a document constructor is not out of the ordinary, but I would suggest making a few revisions to your class to be a bit more intentional in your approach. Take a look at the example below for some ideas of how to class it up a bit (lolz). Remember - both your constructor and your output buffer can be great friends when dealing with document output.

<?php

class PageHandler
{

    private $html;

    function __construct()
    {
        $this->html = $this->getHeader();
    }

    private function getHeader()
    {

        ob_start();
?>
<html>
    <head>
        <title>Nuclear Summer</title>
    </head>
    <body>
<?php
        return ob_get_clean();

    }

    private function getFooter()
    {

        ob_start();
?>
    </body>
</html>
<?php
        return ob_get_clean();

    }

    function setBody($html)
    {
        $this->html .= $html;
    }

    function getDocument()
    {
        return $this->html . $this->getFooter();
    }

}

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜