PHP Web Controls

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hey all,

ASP.NET has a concept of a user defined web control, where a person
can implement their own web control and override the render method to
write out html for that control.

I was wondering, in general, do people ever follow a similar pattern
when writing PHP4+ code? By which i mean, i could write a WebControl
base class and a TextBoxControl that inherits from that, then in my
html markup instead of writing out the input tag by hand i could call

Up to this point i have simply hard coded all the html myself and then
inserted echos into the appropriate places. However, im wondering if
this might be a better approach then having such "messy" html.

Any thoughts? Opinions?

Re: PHP Web Controls

Quoted text here. Click to load it

This is the only thing I miss when I code PHP. makes template
logic so much simpler without a few extra tons of code. In PHP, we can
use libraries such as Smarty and others that work similarly to achieve
something similar. In fact, this method at times allows more control
over what goes on than controls do.

Here is Smarty template engine, a powerful templating engine that is
basically setting the standard for other engines:

And here is TemplateLite, which works almost exactly the same as
Smarty (you barely have to change any code when switching between this
and Smarty), but is better performance-wise. They try to make it so it
can work with the same template files as does Smarty, although
accessing the superglobals works a bit differently. Also, when you
make your own functions, modifiers, and blocks (similar to controls),
the naming convention of the functions is a bit different.

I think it's good to work with templating engines that work like
Smarty because spin-offs often try to be compatible with it. This way,
if you ever want to switch which your site runs off of, it shouldn't
be too hard.

Mike PII

Re: PHP Web Controls

Areric wrote:

Quoted text here. Click to load it

Sometimes, sometimes not. They can be useful for projects when you need to
write a lot of forms. For just a small, one-off form though, they're often
more trouble that they're worth.

There are a few pre-written form libraries here:

And I've written one here:

It's good for tab-based forms -- see the comments form on my website. It's
far from complete -- the most significant missing feature is the ability
to feed back error messages from server-side validation. It has a few nice
features though, like client-side validation backed up by revalidation on
the server.

This is somewhat more complete:

It's more suited towards long surveys.

Toby A Inkster BSc (Hons) ARCS
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

Re: PHP Web Controls


on 05/02/2007 02:02 PM Areric said the following:
Quoted text here. Click to load it

You may want to try this forms generation class. You can develop
plug-ins to implement new behaviors, new validations, new layouts, etc..

Here you may find several example scripts in action so you can see the
basic form features and several plug-ins:

Here is a tutorial video explain all the features and some of the
available plug-ins:


Manuel Lemos

Metastorage - Data object relational mapping layer generator

PHP Classes - Free ready to use OOP components written in PHP

Re: PHP Web Controls

Thanks all,

Ill check out those various libraries. I once sat down to try and
write my own controls library but really just wondered what it would
gain me. The one nice thing was the ability to use inheritance with
individual controls, so say for example i had a text box control, i
could then derive a numeric text box that did all the validation work
to ensure the user input numeric values. Its a lot more readable than
having a select statement with all the javascript wireup in a million

Re: PHP Web Controls

Quoted text here. Click to load it

Are form controls the only kind of controls you like to use in

-Mike PII

Re: PHP Web Controls

Oh yea, how about Java integration? Maybe you can use JSF UI controls.
They are sort of the Java equivalent of controls.

-Mike PII

Re: PHP Web Controls

Areric schreef:
Quoted text here. Click to load it

Of course! I call them widgets, here is there class hierarchy:

Some special widgets are:
- DialogWidget: holds a reference to an object. It lets the user open a  
dialog to search and select an object to reference. To try it out go to:
Then click in the widget behind 'country'. After you have selected a  
different coutry you will see the widget shows the name of the country  
you just selected. If you then press the 'Update' button the form will  
be submitted, all it values validated and stored, including the  
reference to the county (visitors are not allowed to do this, but if you  
enter invalid values the validation will produce error messages)

- MtoNDialogWidget holds references to multiple objects. It lets the  
user open an MtoNDialog where the user can add and remove references.  
After the dialog is closed the state of the widget reflects the choices  
the user made. If you then press the 'Update' button the form will be  
submitted, causing the relation in the database to be updated. To try it  
out go:
and click in the widget behind 'keywords'.

Of course the dialogs hemselves as well as all other pages are built  
following the same render pattern, only these are composite  
componenents, the composition pattern is explained here:

Quoted text here. Click to load it
It is really worthwile following these patterns, your code becomes much  
more readable and you can easily reuse reoccuring components. You do not  
need a framework to do so, but if you take time to learn how to use the  
framework it may significantly boost your productivity.


Henk Verhoeven,

Site Timeline