Home arrow PHP arrow Page 2 - 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 - Analysis
(Page 2 of 7 )

Let's take a look at the task of processing user forms and try to find the most common steps. They will describe generic functionality of a form. First of all, a form must be presented to the user. This includes rendering graphical representation of form objects (which is carried out by browsers quite efficiently). Then we must display the initial values; this should be governed by the application logic. Then the user's input must be received and analysed. We receive a set of values from the user. At the abstract level the decision that is made on this stage would usually be: can this form be considered completed or should we go back and ask user to correct something?

Let's first look at the situation when we need something corrected. In this case an application must display the form again and give some explanations about what went wrong (display some error messages). Besides, a well-mannered form should preserve values entered by the user, at least the correct ones.

Now suppose, we decide that the values are good enough to allow us to continue. Then these values (or some of them) usually get stored somehow (in a database, in session variables, etc.).

Finally, another decision is made: where do we go from here? It is often based on values entered and, of course, follows some paths defined by the programmer.

Now, what should we try to achieve by the object analysis? The best would be to program generic behaviour in parent class(es) once and forever. Then from that moment on we will be able to only concentrate on context-specific steps for each new form that we develop: check values, store values, decide where to go from the form.

The key element will be a generic form class that I called FormProcessor. Let's first get a general idea about how it works. I like putting forms into separate files. There's nothing that makes it necessary, it just makes things easier, for example, to edit them in visual editors. Also, I usually have forms that 'submit to themselves', in other words I usually group a form with the code that takes its input.

One idea is to create instances of FormProcessor's sub-classes in the beginning of such files. Then, this instance will 'tune in' the environment and will be able to have complete control over displaying the form and working with it further on. By 'tuning in', I mean deciding whether this particular form was displayed and submitted before and we should now check the input. If it has never been submitted before, it should be displayed for the first time. Object-oriented technology will work for us in both situations.

Generic framework within FormProcessor will run one of the virtual member functions -- either for checking or for displaying the form.

Notice: all we will have to do in each particular form's class is override these virtual functions. In the same way, we will override the function responsible for storing form's values. Again, the generic class will decide when to call it.

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