Home arrow PHP arrow Page 5 - Making PHP Forms Object-Oriented

Making PHP Forms Object-Oriented

In this article Yuri explains an object oriented way to create and use forms on your web site.

Author Info:
By: Yuri Makassiouk
Rating: 4 stars4 stars4 stars4 stars4 stars / 40
January 23, 2003
  1. · Making PHP Forms Object-Oriented
  2. · Analysis
  3. · Executing Environment
  4. · Implementing the FormProcessor Class
  5. · Inputs - Brief Introduction
  6. · Example of a Form
  7. · Conclusion

print this article

Making PHP Forms Object-Oriented - Inputs - Brief Introduction
(Page 5 of 7 )

In fact, auto-assigning inputs' names, really was the initial idea that pushed me towards developing of another set of classes. The classes that would serve as object wrappers for HTML form's input controls. In this article I would really like us to concentrate on the FormProcessor class and its descendants. Developing a properly analysed hierarchy of classes for form controls deserves a separate discussion. So, I suggest that we only talk about form controls classes as much as we need for completing this study.


  * A small hierarchy of classes that
  * serve as object wrappers for HTML form's inputs

  * An abstract class representing generic form control
  * @author Yuri Makassiouk <racer@bfpg.ru>,
  *  Mission & Media <firma@missionandmedia.com>
  class FormControl {
    var $Name;
    var $form;
    var $Value;
    var $Attributes;
    var $FormName;
    var $InputName;
    function FormControl($Aform, $AName, $AValue='', $AAttributes='') {
      $this->Name = $AName;
      $this->form = $Aform;
      $this->Value =  
      $this->Attributes = $AAttributes;
      $this->FormName = $Aform->Name;
      $this->InputName = sprintf("%s%s[%s]",  
        $this->form->fieldPrefix, $this->FormName, $this->Name);

    function InputName() {
      echo $this->InputName;
    function Display() {
      echo $this->Render();
    function Render() {}
  * Class representing a text control
  class TextInput extends FormControl {

    function Render() {
      return "<input type=\"Text\" name=\"".
        $this->InputName."\" value=\"$this->Value\" $this->Attributes>";

  * Class representing a set of radio buttons
  class RadioButtons extends FormControl {
    var $options;
    var $ItemFormat = '%RBTN %LABEL';
    var $separator = '<br>';
    function RadioButtons($Aform, $AName,  
        $OptionList, $AValue='', $AAttributes='',  
        $AItemFormat='%RBTN %LABEL', $Aseparator = '<br>') {
      $this->options = $OptionList;
      $this->ItemFormat = $AItemFormat;
      $this->separator = $Aseparator;
      $this->FormControl($Aform, $AName, $AValue, $AAttributes);

    function Render() {
      $out = '';
      while (list($key, $val)=each($this->options)) {
        $item = $this->ItemFormat;
        $item = str_replace('%RBTN',  
            sprintf("<input type=\"Radio\" name=\"%s\"  
            value=\"${key}\" $this->Attributes%s>",  
            $this->InputName, $key==$this->Value?' checked':''), $item);
        $item = str_replace('%LABEL', $val, $item);
        if (++$i!=count($this->options))
          $item .= $this->separator;
        $out .= $item;
      return $out;


As you can see, the constructor of the base class (FormControl) does all the job of linking the control to form object, reference to which is passed as one of parameters. Then, the name of HTML input is evaluated. It should be such that the value will be submitted as part of an array which the form (descendant of FormProcessor) will be looking for. The name of input is then stored into a local data member. The control also receives the default value as one of constructor's parameters. This value will be used if the form that contains the control is displayed for the first time. The control is accessing a data member of form class to check this. Otherwise, the default value will be taken from the form's $Values array.

Then the constructor calls the Display() member function, which echoes output of the Render() function. This function is abstract and should be overridden in sub-classes so that they produce correct HTML code to represent themselves in pages.
See the definitions of the two other classes: TextInput and RadioButtons for an illustration of this.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials