Use an object-oriented graphic calculator

Source: Internet
Author: User
: This article describes how to use an object-oriented graphic calculator. For more information about PHP tutorials, see. This example may not be practical, but basically summarizes three features of object-oriented: inheritance, encapsulation, and polymorphism. The main functions of this example are as follows:

  1. Allows you to select different types of images;
  2. Enter relevant attributes for the selected image;
  3. Calculate the length and area of the graph based on the input attributes.

The effect is as follows:

Ideas:

    1. Part A is written directly in index. php, and A $ _ GET ["shape"] is sent to its own page when you click the corresponding graph. the automatic loading class is used.
    2. Part B is composed of form. class. php output, where variable functions are used, and different functions are called with the value of $ _ GET ["shape"] to determine the input part of the forms in different graphs.
    3. The C part is output by result. class. php. Declare an abstract class. in rect, triangle, and circle, calculate the area and perimeter of the abstract class, reflecting inheritance, encapsulation, and polymorphism, use new $ _ GET ["shape"] () to instantiate the object of the corresponding image, and then call the method in the object to return the length of week and area.

Areas for improvement:

  1. This example only serves as a demonstration class and does not filter user input, which may cause injection attacks and is not applicable to actual production applications. In actual applications, user input should be filtered to prevent malicious attacks.
  2. The page layout is not optimized using DIV + CSS, and the interface is not very friendly. The layout can be optimized to improve user experience.

The index. php code is as follows:

123
 456

7. graph perimeter area calculator 8 9 rectangle 10 triangle 11 round 12

13

14 32

3334

The form. class. php code is as follows:

1
 FormAction = $ action; // Save the input parameter to $ formAction; 16 $ this-> shape = isset ($ _ GET ["shape"])? $ _ GET ["shape"]: "rect"; // Obtain the image category from the variables passed in the form. If no image category is transmitted, the default value is 17} 18 function _ toString () {19 $ form = ''; 2627 return $ form; 28} 29 // private method, returns the string of the input part of the rectangle form. 30 privatefunction getRect () {31 // The input content is displayed after the form is submitted, prevent it from disappearing 32 $ formheight = isset ($ _ POST ['height'])? $ _ POST ['height']: NULL; 33 $ formwidth = isset ($ _ POST ['width'])? $ _ POST ['width']: NULL; 34 $ input ='

Enter the length and width of the rectangle.

'; 35 $ input. =' height of the rectangle:

'; 36 $ input. =' width of the rectangle:
'; 37 return $ input; 38} 39 // return the string 40 privatefunction getTriangle () {41 // displayed after the form is submitted, prevent it from disappearing 42 $ formside1 = isset ($ _ POST ['include1'])? $ _ POST ['include1']: NULL; 43 $ formside2 = isset ($ _ POST ['include2'])? $ _ POST ['side2']: NULL; 44 $ formside3 = isset ($ _ POST ['side3 '])? $ _ POST ['side3 ']: NULL; 45 $ input ='

Enter Three sides of the triangle

'; 46 $ input. =' side length 1:

'; 47 $ input. =' side length 2:

'; 48 $ input. =' side length 3:
'; 49 return $ input; 50} 51 // return the string 52 privatefunction getCircle () in the input part of the circular form () {53 $ formradius = isset ($ _ POST ['radius '])? $ _ POST ['radius ']: NULL; // The content is displayed after the input form is submitted to prevent it from disappearing. 54 $ input ='

Enter the radius

'; 55 $ input. =' radius:
'; 56 return $ input; 57} 58} 59

Result. class. php code:

1
 Shape = new $ _ GET ["shape"] (); 8} 9 // call the attributes and methods of an object, and return the length and area of 10 function _ toString () {11 $ result = $ this-> shape-> shapeName. 'perimeter is '. $ this-> shape-> perimeter ().'
'; 12 $ result. = $ this-> shape-> shapeName.' The area is '. $ this-> shape-> area ().'
'; 13 return $ result; 14} 15}

The abstract class shape. class. php code is as follows:

1
 ShapeName. $ message; 20 echo ''. $ message. 'must be a positive number.
'; 21 returnFALSE; 22} 23else24returnTRUE; 25} 26}

The code of the subclass triangle. class. php is as follows:

1
 ShapeName = "triangle "; // named image 1718 // use the method of the parent class validate to check whether the input is a positive number 19if ($ this-> validate ($ _ POST ["side1"], "side length 1") & $ this-> validate ($ _ POST ["side2"], "side length 2 ") & $ this-> validate ($ _ POST ["side3"], "side length 3 ")) {2021 // verify whether the two sides and the third side are greater than 22if ($ this-> validatesum ($ _ POST ["side1"], $ _ POST ["side2"], $ _ POST ["side3"]) {23 $ this-> side1 = $ _ POST ["side1"]; // if the three sides are initialized through verification; 24 $ this-> side2 =2 _ POST ["side2"]; 25 $ this-> side3 =$ _ POST ["side3"]; 26} 27 else {28 echo 'the sum of the two sides must be greater than the third edge'; 29 exit (); 30} 31} 32 else {33 exit (); 34} 35} 36/* Use Helen formula to calculate the area and return the result */37 function area () {38 $ s = ($ _ POST ["side1"] + $ _ POST ["side2"] + $ _ POST ["side3"])/2; 39 returnsqrt ($ s * ($ s-$ _ POST ["side1"]) * ($ s-$ _ POST ["side2"]) * ($ s-$ _ POST ["side3"]); 40} 41/* calculates and returns the circumference */42 function perimeter () {43 return $ _ POST ["side1"] + $ _ POST ["side2"] + $ _ POST ["side3"]; 44} 45/* calculate whether the two sides of a triangle are greater than the third side. TRUE is returned; FALSE is returned */46 privatefunction validatesum ($ side1, $ side2, $ side3) {47if ($ side1 + $ side2)> $ side3 & ($ side1 + $ side3)> $ side2 & ($ side2 + $ side3)> $ side1) 48 returnTRUE; 49else50returnFALSE; 51} 52}

The code for subclass circle. class. php is as follows:

1
 ShapeName = "circle"; 13if ($ this-> validate ($ _ POST ["radius"], "radius ")) 14 $ this-> radius =$ _ POST ["radius"]; 15} 16 // return area of the circle 17 function area () {18 return 3.14 * $ this-> radius; 19} 20 // returns the circle perimeter 21 function perimeter () {22 return 3.14*2 * $ this-> radius; 23} 24}

The code for subclass rect. class. php is as follows:

1
 ShapeName = "rectangle"; 14if ($ this-> validate ($ _ POST ["width"], "width ") & $ this-> validate ($ _ POST ["height"], "height") {15 $ this-> width =$ _ POST ["width"]; 16 $ this-> height = $ _ POST ["height"]; 17} 18} 19 // return area 20 function area () {21 return $ this-> width * $ this-> height; 22} 23 // returns the circumference 24 function perimeter () {25 return 2 * ($ this-> width + $ this-> height); 26} 27}

Statement:

1. This article is only applicable to experiments and is not suitable for practical applications. I am not responsible for any adverse consequences.

2. This article is an original blog, which can be freely reproduced on the personal platform. However, you need to indicate the source and attach a link. Otherwise, it will be deemed as theft. It is strictly prohibited to be used for commercial purposes. if necessary, contact me to pay the draft fee and authorize it for use.

The above introduces the use of object-oriented graphics calculators, including the content, hope to be helpful to friends interested in PHP tutorials.

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.