开发者

How can i improve this code using Object Oriented Programming?

Is this is far as you would go regarding Object Oriented PHP or can i improve it further by putting HTML into a class? How would i go about doing that? Note: The code below is using a QuickForm by PEAR. Thanks in advance.

  <?php
    //MySQL class
    require_once('database/class.mysql.php');

    //Session class
    require_once('session/class.session.php');

    //SignUp class
    require_once('access/class.signup.php');

    //QuickForm class
    require_once('HTML/QuickForm.php');

    //PHPMailer class
    require_once('thirdparty/phpmailer/class.phpmailer.php');

    //Instantiate the MySQL class
    require_once('database/dbconnect.php');
    $db = &new MySQL($host, $dbUser, $dbPass, $dbName);

    //Instantiate the Session class
    $session = new Session;

     ?>

    <html>
        <head>
            <title>Test page</title>
        </head>
        <body>
            <?php
                //Instantiate Quickform
                $form = new HTML_QuickForm('regForm', 'POST');

                ///Header
                $form->addElement('header', null, 'Quickform tutorial example!');

                //First name
                $form->addElement('text', 'fname', 'First name:', array('size' => 30));
                $form->addRule('fname', 'Please enter your first name', 'required', null, 'client');

                //Last name
                $form->addElement('text', 'lname', 'Last name', array('size' => 30));
                $form->addRule('lname', 'Please enter your last name', 'required', null, 'client');

                //Password
                $form->addElement('password', 'pass', 'Password: ', array('size' => 30));

                //Gender
                $form->addElement('radio', 'sex', 'Gender: ', 'Male', 'male');
                $form->addElement('radio', 'sex', '', 'Female', 'female');

                //County
                $form->addElement('select', 'county', 'County:', array(
                    '0' => 'Select',
                    '1' => 'Louth',
                    '2' => 'Meath'
                ));

                //Date of Birth
                $form->addElement('date', 'dob', 'Date of Birth:', array('format' => 'dMY', 'minYea开发者_StackOverflow中文版r' => 1950, 'maxYear' => date('Y')));

                //Submit
                $form->addElement('submit', null, 'Submit');

                //Try to validate the form
                if ($form->validate()) {
                    echo 'Hello';
                }

                //Output the form
                $form->display();
            ?>
        </body>
    </html>


Personally, I wouldn't recommend using any sort of HTML element generating class for your page builds, especially if it also includes dynamically generated javascript.

You might be saying "Dude: what's the difference if it's all ending up as HTML anyway?"

If you do, you're left with an archaic code project by next year considering how often standards change. Sure an element like a is not likely to be depreciated any time soon, but it's base and style attributes likely will be. You've also got new special-case elements that you have to programatically account for, such as canvas. You try and validate your HTML and discover that the br and a few other tags suddenly don't accept a closing slash, let alone a closing tag. By the time you get to handling CSS in your element-generating class, you'll likely realized you just wasted all this time.

Sure programming PHP is most of the time nothing more than mixing HTML and scripting, but it's always easier for some random, inexperienced web admin to come in after you and update HTML to be current standards compliant rather than blindly digging through your PHP source. Any time there's an update anywhere in your stack or a web standard, you personally have to upgrade and update everything throughout your entire project to make sure a) your HTML-generator class still functions; b) it can handle every known HTML element and Javascript function to support every known browser. Normally, you shouldn't need to worry about updating PHP and HTML/Javascript at the same time.

So, use PHP to handle backend DB, remote fetches, user authentication, form validation, file management, and math calculations, but use caution when dedicating it to handle HTML structure.

Just IMHO. Templating is good though, since that's fundamentally what PHP website design is.


You may want to look into using a PHP templating system like Smarty. This will allow you to separate your HTML from the PHP code entirely. It also has added benefits of allowing you to create standard headers, footers, menus, etc. for all your pages.

The Smarty templating system in particular seems to be written with good OOP principles. You create a Smarty object, call the assign() method to inject information in a safe way, then use the display() method to generate your HTML.


If you want to develop web applications using object-oriented principles and other best practices, your best bet is to look into an MVC framework such as (for PHP) Symfony or CakePHP.

As well as a strong object-oriented approach with a templating framework, they are also designed with other best practices in mind such as unit testing, or object/relational mapping for database access.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜