note 28620 added to language.oop
| From: | mcoakley at netplay dot net | Date: | Sat, 18 Jan 2003 02:48:36 +0000 |
| Subject: | note 28620 added to language.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-42328@lists.php.net to get a copy of this message | ||
Here is the example I promised...
//Abstract class - should never be instantiated
class FormField {
var $name;
var $value;
var $style;
construction FormField($name, $value, $style = "") {
$this->name = $name;
$this->setValue($value);
$this->setStyle($style);
}
function setValue($value) { $this->value = $value; }
function getValue() { return $this->value; }
function getName() { return $this->name; }
function setStyle($style) { $this->style = $style; }
function getStyle() { return $this->style; }
//Polymorphism - this method is the main polymorphic point
function displayField() { echo "YOU MUST OVERRIDE!!!"; }
function getDisplayStyle() {
if ($this->style=="") {
return "";
} else {
return " style=\"$this->style\"";
}
}
class TextInputField extends FormField {
var $maxlength;
var $size;
function TextInputField($name, $value, $style = "", $maxlength = 255, $size = 20) {
$this->FormField($name, $value, $style);
$this->maxlength = $maxlength;
$this->size = $size;
}
function displayField() {
echo "<input name=\"$this->name\" size=\"$this->size\"
maxlength=\"$this->maxlength\"" . $this->getDisplayStyle() . "
value=\"$this->value\">";
}
}
//Another abstract class
class CheckboxRadioField extends FormField {
var displayName;
var selected;
function CheckboxRadioField($name, $value, $displayName, $style = "") {
$this->FormField($name, $value, $style);
$this->displayName = $displayName;
}
function setSelected($selected) { $this->selected = $selected; }
function isSelected() { $this->selected; }
function getDisplaySelected() {
if ($this->isSelected()) {
return " checked";
} else {
return "";
}
}
}
class CheckboxField extends CheckboxRadioField {
function CheckboxField ($name, $value, $displayName, $style = "") {
$this->CheckboxRadioField($name, $value, $displayName, $style);
}
function displayField() {
echo "<input type=\"checkbox\" name=\"$this->name\"
value=\"$this->value\"" . $this->getDisplaySelected() .
$this->getDisplayStyle() . ">$this->displayName";
}
}
class RadioField extends CheckboxRadioField {
function RadioField ($name, $value, $displayName, $style = "") {
$this->CheckboxRadioField($name, $value, $displayName, $style);
}
function displayField() {
echo "<input type=\"radio\" name=\"$this->name\"
value=\"$this->value\"" . $this->getDisplaySelected() .
$this->getDisplayStyle() . ">$this->displayName";
}
}
//Now a quick usage example
$fields[] = new TextInputField("text1", "Hello World", "", 40);
$fields[] = new CheckboxField("checkbox1", "option_1", "Click here for
option #1");
$fields[] = new RadioField("radio1", "rad_option_1", "Polished");
$fields[] = new RadioField("radio1", "rad_option_2", "Rough");
echo "<form action=\"form_handler.php\" method=\"post\">";
for ($i = 0; $i < count($fields); $i++) {
//Polymorphism at its best
$fields[$i]->displayField();
}
echo "</form>";
Sorry for the long post but I just thought it could help...
--
http://www.php.net/manual/en/language.oop.php
http://master.php.net/manage/user-notes.php?action=edit+28620
http://master.php.net/manage/user-notes.php?action=delete+28620
http://master.php.net/manage/user-notes.php?action=reject+28620