pinxxx Posted August 25, 2009 Share Posted August 25, 2009 i just started to read about object-oriented programming using php. i took a code that will print some message but when i tried to open it, it doesn't show anything.. is there something wrong with my code or i just forget something? here's the code from a book.. please help. thanks in advance! <?php /* * Class name: Form * Description : A class that creates a simple HTML form * containing only text input fields. The class has 3 methods. */ class Form { var $fields=array(); # contains field names and labels var $processor; #name of program to process form var $submit = "Submit Form"; # value for the submit button var $Nfields = 0; # number of fields added to the form /* Constructor: User passes in the name of the name of the script where *form data is to be sent ($processor) and the value to show on the submit button. */ function __construct($processor, $submit) { $this->processor = $processor; $this->submit = $submit; } /* Display form function. Displays the form. */ function displayForm() { echo "<form action='$this->processor)' method='post'>"; echo "<table width='100%'>"; for($j=1;$j<=sizeof($his->fields);$j++) { echo "<tr><td align=\"right\"> {$this->fields[$j-1]['label']}: </td>\n"; echo "<td> <input type='text' name='{$this->fields[$j-1]['name']}'> </td></tr>\n"; echo "<tr><td colspan=2 align='center'> <input type='submit' value='{$this->submit}'></td></tr>\n"; echo "</table>"; } /* Function that adds a field to the form. The user needs to * send the name of the field and a label to be displayed. */ function addField($name,$label) { $this->fields[$this->Nfields]['name'] = $name; $this->fields[$this->Nfields]['label'] = $label; $this->Nfields = $this->Nfields + 1; } } } ?> Link to comment https://forums.phpfreaks.com/topic/171747-new-in-oop/ Share on other sites More sharing options...
trq Posted August 25, 2009 Share Posted August 25, 2009 Where are you instantiating and actually using this class? Link to comment https://forums.phpfreaks.com/topic/171747-new-in-oop/#findComment-905596 Share on other sites More sharing options...
oni-kun Posted August 25, 2009 Share Posted August 25, 2009 Yes, for example you've built the class and now you need to place it into an object! $form = new Form('form.php', 'Submit!'); //Create new object with constructor $form->addfield('this','that'); //Use a object function $form->addfield('$date', '$time') $form->displayForm(); //Use object's class function to display form Code is only an example, but as you see you call the functions within the class, and instantiate it from an object. Link to comment https://forums.phpfreaks.com/topic/171747-new-in-oop/#findComment-905599 Share on other sites More sharing options...
pinxxx Posted August 25, 2009 Author Share Posted August 25, 2009 @thorpe: i just got the code from the book i'm reading and i want to test the output of this code. @oni-kun: thanks for the explanation! i kinda got it. i'll go back to the sample code and i'll try it again. thanks for the help.. Link to comment https://forums.phpfreaks.com/topic/171747-new-in-oop/#findComment-905608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.