I wrote a program for calculating the graphic area according to the book, but did not perform the calculation. what is the last example of the error in chapter 8th of php. You can create a form, but an error occurs after you click calculate. Thank you!
Interface: class _ Shape. php
Interface Shape {
Function area ();
Function perimeter ();
}
?>
Rectangle: class _ Rect. php
Class Rect implements Shape {
Private $ width;
Private $ length;
Function _ construct ($ size = ""){
$ This-> width = $ size ["width"];
$ This-> length = $ size ["length"];
}
Function area (){
Return $ this-> length * $ this-> width;
}
Function perimeter (){
Return 2 * ($ this-> width + $ this-> length );
}
}
?>
Triangle: class _ Triangle. php
Class Triangle implements Shape {
Private $ length1;
Private $ leng2;
Private $ length3;
Function _ construct ($ size = ""){
$ This-> length1 = $ size ["length1"];
$ This-> lengh2 = $ size ["leng2"];
$ This-> length3 = $ size ["length3"];
}
Function area (){
$ S = ($ this-> length1 + $ this-> length1 + $ this-> length3)/2;
Return sqrt ($ s * ($ s-$ this-> length1) * ($ s-$ this-> length1) * ($ s-$ this-> length3 ));
}
Function perimeter (){
Return $ this-> length1 + $ this-> length1 + $ this-> length3;
}
}
?>
Circle: class _ Circle. php
Class Circle implements Shape {
Private $ radius;
Function _ construce ($ size = ""){
$ This-> radius = $ size ["radius"];
}
Function area (){
Return pi () * $ this-> radius;
}
Function perimeter (){
Return 2 * pi () * $ this-> radius;
}
}
?>
Table: class _ Form. php
Class Form {
Private $ formName;
Private $ request;
Private $ action;
Private $ method;
Private $ target;
Function _ construct ($ formName, $ request, $ action = "index. php", $ method = "get", $ target = "_ self "){
$ This-> formName = $ formName;
$ This-> request = $ request;
$ This-> action = $ action;
$ This-> method = $ method;
$ This-> target = $ target;
}
Function _ toString (){
$ Str ="
";$ Str. ="
". $ This-> formName ."
";
$ Str. ="
";
Return $ str;
}
}
?>
Control Program: index. php
Graphics calculator
Function _ autoload ($ className ){
Include ('class _ '. ucfirst ($ className).'. php ');
}
?>
Graph length and area calculator
Rectangle |
Triangle |
Circle
Switch ($ _ REQUEST ["action"]) {
Case 1:
$ Form = new Form ("rectangle", $ _ REQUEST, "index. php ");
Echo $ form;
Break;
Case 2:
$ Form = new Form ("triangle", $ _ REQUEST, "index. php ");
Echo $ form;
Break;
Case 3:
$ Form = new Form ("circle", $ _ REQUEST, "index. php ");
Echo $ form;
Break;
Default:
Echo "select a graph
";
}
If (isset ($ _ REQUEST ["act"]) {
Switch ($ _ REQUEST ["act"]) {
Case 1:
$ Shape = new Rect ($ _ REQUEST );
Break;
Case 2:
$ Shape = new Triangle ($ _ REQUEST );
Break;
Case 3:
$ Shape = new Circle ($ _ REQUEST );
Break;
}
Echo "area:". $ shape-> area ()."
";
Echo "perimeter:". $ shape-> perimeter ()."
";
}
?>
Reply to discussion (solution)
Nobody looks at it? Haven't you woken up yet?
In class _ Form. php
$ Str. ="";
Return $ str;
}
}
?>
Index. php:
<Br/> calculate the area and perimeter of the image <br/>
Function _ autoload ($ className) {// automatically loaded to this page when it is included in the class
Include ("class _". ucfirst ($ className). ". php"); // automatically load the file where the corresponding class is located.
}
?>
Rectangle |
Triangle |
Circle
Switch ($ _ REQUEST ["action"]) {
Case 1:
$ Form = new Form ("rectangle", $ _ REQUEST, "index. php ");
Echo $ form;
Break;
Case 2:
$ Form = new Form ("triangle", $ _ REQUEST, "index. php ");
Echo $ form;
Break;
Case 3:
$ Form = new Form ("circle", $ _ REQUEST, "index. php ");
Echo $ form;
Break;
}
If (isset ($ _ REQUEST ["act"]) {
Switch ($ _ REQUEST ["act"]) {
Case 1:
$ Shape = new Rect ($ _ REQUEST );
Break;
Case 2:
$ Shape = new Triangle ($ _ REQUEST );
Break;
Case 3:
$ Shape = new Circle ($ _ REQUEST );
Break;
}
Echo "area:". $ shape-> area ()."
";
Echo "perimeter:". $ shape-> perimeter ()."
";
}
?>
Other code remains unchanged. what is the problem ??
Class _ Form. php
formName=$formName;$this->request=$request;$this->action=$action;$this->method=$method;$this->target=$target;}function __toString(){$str="
";$str.="
".$this->formName."
";$str.="
";return $str;}}?>Index. php
Graphics calculator
Rectangle of the graph's circumference and area calculator | triangle | circle
";} If (isset ($ _ REQUEST [" act "]) {switch ($ _ REQUEST [" act "]) {case 1: $ shape = new Rect ($ _ REQUEST); break; case 2: $ shape = new Triangle ($ _ REQUEST); break; case 3: $ shape = new Circle ($ _ REQUEST); break;} echo "area :". $ shape-> area ()."
"; Echo" perimeter: ". $ shape-> perimeter ()."
";}?>
My eyes! Done. Made a very low-level mistake.