PHP
  Home arrow PHP arrow Page 5 - Making PHP Forms Object-Oriented
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Making PHP Forms Object-Oriented
By: Yuri Makassiouk
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2003-01-23

    Table of Contents:
  • Making PHP Forms Object-Oriented
  • Analysis
  • Executing Environment
  • Implementing the FormProcessor Class
  • Inputs - Brief Introduction
  • Example of a Form
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    <?php

      /**
      * 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->form->FirstTime)?$AValue:($this->form->Values[$this->Name]);
          $this->Attributes = $AAttributes;
          $this->FormName = $Aform->Name;
          $this->InputName = sprintf("%s%s[%s]",  
            $this->form->fieldPrefix, $this->FormName, $this->Name);

          $this->Display();
        }
        
        function InputName() {
          echo $this->InputName;
        }
        
        function Display() {
          echo $this->Render();
        }
        
        function Render() {}
        //abstract
      }
      
      /**
      * 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() {
          $i=0;
          $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.

    More PHP Articles
    More By Yuri Makassiouk


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT