The sixth article in the object-oriented series of PHP-Implementation of a simple graphic area calculator and the sixth article on object orientation

Source: Internet
Author: User

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 ;}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.