Liquid Fire Posted April 10, 2007 Share Posted April 10, 2007 I was doing some research on factory clas pattern and while doing this i have created some form classes. Now there was never and expectation of keeping this code but i thought it just might be useful. Basically here are 2 files including with this class: <?php class CTextField { /** * @desc Creates the form text field HTML and stores it. * @param mixed[] $attributes This will hold all attributes in an array */ private function __construct($attributes) { //make sure there is no data in this $this->_html_output = null; $this->_html_output = "<input type=\"text\""; foreach($attributes as $attribute => $value) { $this->_html_output .= " {$attribute}=\"{$value}\""; } $this->_html_output .= " />"; } /** * @desc Return the HTML code needed to think the form field; */ public function HTMLOutput() { return $this->_html_output; } private $_html_output; } ?> so basically the code to get the html to display a form fiels would be: <?php //this is the class the creates the class and return the html needed to display - sorry i don't have the code for that class, it is at my house and right nwo am at work $form_element_creator = new CFormElementCreator(); $name_field_attributes = array("name" => "name", "size" => 30); $name_field = $form_element_creator->NewElement('CTextField', $name_field_attributes); ?> <form> <table> <tr><td><?php echo $name_field; ?></td></tr> </table> </form> The reason i like this is i can seperate my form element and the code for displaying the form elements and for creating them is much cleaner IMO. Also this will make sure the the form field will never have any problem like i sometimes forget the /> and just have > which i think is a XHTML validation error. Also i can look at my feild attributes alot better becuase if i have form of 30+ field and i write it all out in HTML sometimes it is hard to locate the 17th feild but this way all the field and right below each is a nice order. Do you think this is a over kill, i don't but maybe it is. Link to comment https://forums.phpfreaks.com/topic/46460-is-this-a-over-kill/ Share on other sites More sharing options...
Liquid Fire Posted April 10, 2007 Author Share Posted April 10, 2007 Thinking about it i really don't think it is going to add much overhead with make pages with forms noticable slowwer so i think i am goin gto build this out and see how it works. Link to comment https://forums.phpfreaks.com/topic/46460-is-this-a-over-kill/#findComment-226203 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.