Home arrow PHP arrow Page 4 - Creating a Static Web Form Helper with Restrictive Constructors

Using the previous Form helper class - PHP

Welcome to the final installment of a series that shows you how to use restrictive constructors in PHP 5. With a decent variety of code samples, this series recreates for you a few common scenarios where the implementation of both protected and private constructors can greatly improve the behavior of different classes in the context of a given application.

TABLE OF CONTENTS:
  1. Creating a Static Web Form Helper with Restrictive Constructors
  2. Review: using a private constructor in a Singleton class
  3. Building a static web form helper class with a private constructor
  4. Using the previous Form helper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 23, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you’re anything like me, then you’ll want to see an example that shows how to use the purely static “Form” helper class. Below I coded a script that employs the helper to build an HTML form that collects data on a fictional user. Here it is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <title>Using the static web form helper</title>

    </head>

    <body>

        <h1>Using the static web form helper</h1>

       <?php echo Form::open(array('action' => 'create', 'method' => 'post'));?>

       <p>First Name: <?php echo Form::input(array('type' => 'text', 'name' => 'fname'));?></p>

       <p>Last Name: <?php echo Form::input(array('type' => 'text', 'name' => 'lname'));?></p>

       <p>Email: <?php echo Form::input(array('type' => 'text', 'name' => 'email'));?></p>

       <p><?php echo Form::input(array('type' => 'submit', 'name' => 'send', 'value' => 'Create user'));?></p>

       <?php echo Form::close();?>

    </body>

</html>

As the above script shows, it’s extremely easy to build a basic web form by using the static methods provided by the “Form” helper class. Yet, the most important thing to note here is that any attempt to instantiate the class will result in a fatal error being triggered by the PHP engine, thanks to the declaration of its unimplemented private constructor.

And with this final example, I’m finishing this last tutorial of the series. As always, feel free to introduce your own tweaks to all of the sample classes shown, so you can arm yourself with a better background in using restrictive constructors in PHP 5.

Final thoughts

Sad to say, we’ve come to the end of this series. Hopefully, the examples deployed in its tutorials have provided you with the right pointers to help you start using restrictive constructors within your own PHP 5 classes (when applicable, of course).

As you learned in previous examples, even a detail as subtle as changing the level of accessibility of your constructors in certain cases may considerably improve the behavior of their originating classes. This is a concept that you should keep in mind when developing your own web applications.

See you in the next PHP tutorial!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: