I want to increase my knowledge of PHP by learning about classes. I wrote the following very simple test program. It works. But I would like to know improve it. I plan on writing more complex test programs and that won't be the time to iron out smaller issues. Thanks in advance.
<?php
$length = array_key_exists('length', $_POST) ? $_POST['length'] : null;
$width = array_key_exists('width', $_POST) ? $_POST['width'] : null;
if ($length == "") $length = 0;
if ($width == "") $width = 0;
?>
<?php
class Shape {
var $length;
var $width;
function setLength($Length) {
$this -> length = $Length;
}
function setWidth($Width) {
$this -> width = $Width;
}
function getPerimeter() {
return 2 * ($this -> length + $this -> width);
}
function getArea() {
return $this -> length * $this -> width;
}
}
$rectangle0 = new Shape;
$rectangle0 -> setLength("$length");
$rectangle0 -> setWidth("$width");
?>
<p> </p>
<p> </p>
<form method=post name="form1" action="practice1.php">
<div class="row">
<label for="winnings">Enter length: </label>
<input style="font-size:12pt" type="text" id="length" name="length" value="<?php if (isset($_POST['length'])) {echo $_POST['length'];} ?>" size="15"/>
<br style="clear:left"/>
<label for="losses">Enter width: </label>
<input style="font-size:12pt" type="text" id="width" name="width" value="<?php if (isset($_POST['width'])) {echo $_POST['width'];} ?>" size="15"/>
<br style="clear:left"/>
</div>
<p> </p>
<p> </p>
<input style="font-size:12pt" type=submit name=submit value="Compute" >
<input type=hidden name=submitted value="true" >
<p> </p>
<p> </p>
<div class="row">
<label for="tax" id="tax" style="font-size:13pt">The perimeter of the rectangle is:</label>
<input style="font-size:12pt" type=text id="perimeter" name="perimeter" value="<?php echo $rectangle0 -> getPerimeter() ?>" size=10 maxlength=10></td>
<br style="clear:left"/><br />
<label for="deficiency" id="deficiency" style="font-size:13pt">The area of the rectangle is:</label>
<input style="font-size:12pt" type=text id="area" name="area" value="<?php echo $rectangle0 -> getArea() ?>" size=10 maxlength=10></td>
<br style="clear:left"/>
</div>
</form>