The sixth article in the object-oriented series of PHP-Implementation of a simple graphic area calculator and the sixth article on object orientation
Previous
This article uses object-oriented technology to implement a simple graphic area Calculator
Graphics
//rect.class.php<?php abstract class Shape{ public $name; abstract function area(); abstract function view(); abstract function test($arr); }?>
Main Interface
// Index. php <! DOCTYPE html>
Rectangle type
// Rect. class. php <? Phpclass Rect extends Shape {private $ width; private $ height; function _ construct ($ arr = []) {if (! Empty ($ arr) {$ this-> width = $ arr ['width']; $ this-> height = $ arr ['height'];} $ this-> name = "rectangle";} function area () {return $ this-> width * $ this-> height;} function view () {$ form = '<form action = "index. php? Action = rect "method =" post "> '; $ form. = $ this-> name. 'width: <input name = "width" value = ""/> <br>'; $ form. = $ this-> name. 'height: <input name = "height" value = ""/> <br>'; $ form. = '<input type = "submit" name = "dosubmit" value = ""> <br>'; $ form. = '</form>'; echo $ form;} function test ($ arr) {$ bg = true; if ($ arr ['width'] <0) {echo $ this-> name. "The width cannot be less than 0! <Br> "; $ bg = false;} if ($ arr ['height'] <0) {echo $ this-> name." The height cannot be less than 0! <Br> "; $ bg = false;} return $ bg ;}}?>
Triangle
// Triangle. class. php <? Phpclass Triangle extends Shape {private $ b1; private $ b2; private $ b3; function _ construct ($ arr = []) {if (! Empty ($ arr) {$ this-> b1 = $ arr ['b1']; $ this-> b2 = $ arr ['b2']; $ this-> b3 = $ arr ['b3'];} $ this-> name = "Triangle";} function area () {$ p = ($ this-> b1 + $ this-> b2 + $ this-> b3)/2; return sqrt ($ p * ($ p-$ this-> b1) * ($ p-$ this-> b2) * ($ p-$ this-> b3);} function view () {$ form = '<form action = "index. php? Action = triangle "method =" post "> '; $ form. = $ this-> name. 'width of the first edge: <input name = "b1" value = ""/> <br> '; $ form. = $ this-> name. 'width of the second edge: <input name = "b2" value = ""/> <br> '; $ form. = $ this-> name. 'Third side width: <input name = "b3" value = ""/> <br> '; $ form. = '<input type = "submit" name = "dosubmit" value = ""> <br>'; $ form. = '</form>'; echo $ form;} function test ($ arr) {$ bg = true; if ($ arr ['b1 '] <0) {echo "the width of the first side cannot be less than 0! <Br> "; $ bg = false;} if ($ arr ['b2'] <0) {echo" the width of the second side cannot be less than 0! <Br> "; $ bg = false;} if ($ arr ['b3'] <0) {echo" the width of the third side cannot be less than 0! <Br> "; $ bg = false;} if ($ arr ['b1'] + $ arr ['b2'] <$ arr ['b3']) | ($ arr ['b1'] + $ arr ['b3'] <$ arr ['b2']) | ($ arr ['b3'] + $ arr ['b2'] <$ arr ['b1']) {echo 'the sum of the two sides cannot be smaller than the third side <br> '; $ bg = false;} return $ bg ;}}?>