I used to look at PHP when I wanted to do this, but there has been no time, this time finally took a sneak into the code more than the amount of project.
First of all, the document structure, all in one folder is good, my example is the following.
Start by entering the index.php file.
Doing a couple of things.1. The number of passes that can be obtained by $get_[the "action" method and the $_request ["action"] method is rect, triangle or circle.
2. The two classes of form.class.php and result.class.php are loaded through the echo new Form ("index.php") and the Echo New Result () method
3. The __tostring method for these two classes is called through ECHO
Then the program loads form.class.php this file
<?phpclass Form {private $action;p rivate $shape, function __construct ($action = "") {$this->action = $action;/* Var_ Dump ($this->action); */$this->shape = Isset ($_request ["Action"])? $_request ["Action"]: "rect";/* Var_dump ($this->shape); The */}/* __tostring () method is used to respond to a class when it is treated as a string. such as Echo $obj; What should be shown. This method must return a string * * output a form here * * */function __tostring () {$form = ' <form action= '. $this->action. ' "method=" post ">" switch ($this->shape) {case "rect": $form. = $this->getrect (); Break;case "triangle": $form. = $this->gettriangle (); Break;case "Circle": $form. = $this->getcircle (); Break;default: $form. = ' Please select a shape ';} $form. = ' <input type= ' "Submit" name= "sub" value= "Calculation" > "$form. = ' </form> '; return $form;} Private Function GetRect () {$input = ' <b> Please enter | The length and width:</b><p> of the rectangle | '; $input. = ' width: <input type= ' text ' Name= "width" value= "'. $_post [' width ']. ' ><br> '; $input. = ' Height: <input type= "test" name= "height" value= "'.$_post [' Height ']. ' ><br> '; $input. = ' <input type= "hidden" name= "action" value= "rect" > "; return $input;} Private Function Gettriangle () {$input = ' <b> input | triangle |:</b><p> '; $input = ' <b> Enter | triangle | Three sides: </b><p> '; $input. = ' first side: <input type= "text" name= "Side1" value= ". $_post [' Side1 ']. ' ><br> '; $input. = ' second side: <input type= "test" name= "Side2" value= ". $_post [' Side2 ']. ' ><br> '; $input. = ' third side: <input type= ' test ' name= ' side3 ' value= '. $_post [' Side3 ']. ' ><br> '; $input. = ' <input type= "hidden" name= "action" value= "triangle" > "; return $input;} Private Function Getcircle () {$input = ' <b> Enter | circle | radius:</b><p> '; $input. = ' radius: <input type= ' text ' Name= "radius" value= "'. $_post [' radius ']. ' ><br> '; $input. = ' <input type= "hidden" name= "action" value= "Circle" > "; return $input;}}? >
Another thing this PHP does is load forms of different types of shapes based on $_request ["action"] or get_["action".Then, once you press the Calculate button, the next step is to load the result.class.php file.
<?php class result{Private $shape; /* * * * * * * * * * * * based on form.class.php $post[' action ' method * */function __construct () { Switch ($_post[' action ']) {case ' rect ': $this->shape=new Rect (); Break Case ' triangle ': $this->shape=new triangle (); Break Case ' Circle ': $this->shape=new circle (); break;//no break will cause default to run as default: $this->shape=false; } } function __tostring () {if ($this->shape) { $result = $this->shape->shapename. ' Perimeter '. $this->shape->perimeter (). ' <br> '; $result. = $this->shape->shapename. ' Area '. $this->shape->area (). ' <br> '; return $result; } else{return ' without this shape '; }}}?>This file did one thing, that is, the diversion, according to $_post["action" passed the value of the view, run that a class file
The next article is about the meaning of each kind of file.
PHP: Brother's Object-oriented graphics Calculator 1