Jump to content

Object oriented form class


VelvetMirror

Recommended Posts

So here is a little sample code I wrote using a class to show a form:

<?phpclass Form {	public function get_form() {		$form = '<form name="form" id="form" class="form" action="manageInput.php" method="post"><label for="name1">Name</label><input name="name" id="name" type="text" /><input name="submit" id="submit" value="Send" type="submit" class="button"/></form>';		return $form;	}}$form = new Form();echo $form->get_form();?>

Which as you can see it is very poorly written. So, how could you write a correct object oriented form class?Is there any sample or tutorial that you would recommend which explains how to correctly write object oriented forms?I know that frameworks like Zend handle this things, but I would prefer not using a framework at the moment.

Link to comment
Share on other sites

What is your idea of a "correct object oriented form class"? The class you have written, while it doesn't do much, is technically correct enough according to the concepts of OO programming.I think you just need to decide on what sort of features you want your class to implement.

Link to comment
Share on other sites

That is a real object, but unless you add more properties and methods, you might as well just use a global function for that. Actually, unless you're going to use that function more than once, it's probably best just to echo out that string on its own. You can save yourself a lot of lines of code and some memory space.

Link to comment
Share on other sites

You can make it generic by adding properties and methods to set things like the form action and method, add new inputs, etc. Here's just a basic example with a dynamic action:

<?phpclass Form {	public $action;	public function __construct()	{		$this->action = '';		}	public function get_form() {		$form = '<form name="form" id="form" class="form" action="' . $this->action . '" method="post"><label for="name1">Name</label><input name="name" id="name" type="text" /><input name="submit" id="submit" value="Send" type="submit" class="button"/></form>';		return $form;	}	public function set_action($a)	{		$this->action = $a;	}}$form = new Form();$form->set_action('process.php');echo $form->get_form();?>

It wouldn't be very difficult to also dynamically add inputs (e.g., give attributes like "type", "name", "value", "label", etc).

Link to comment
Share on other sites

pretty much what JSG said. A class that just returns a one off instance of a form isn't very useful/practical. What is practical/useful is a class that you can instantiate with a config, or have methods for setting different form elements/types, action, method, etc and then a getter for getting the built up output. Basically you would be creating a FormGenerator Class with as many features as you have the capabilities to implement.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...