Jump to content

new in OOP


pinxxx

Recommended Posts

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
Share on other sites

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
Share on other sites

@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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.