• php:兄弟连之面向对象版图形计算器1


     曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project。

    首先,文档结构,都在一个文件夹下就好了,我的就例如以下。


    一開始,进入index.php文件。

    <html>
    
    <head>
    
    <title>图形计算器(面向对象)</title>
    
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    
    </head>
    
    <body>
    
    	<center>
    
    		<h1>
    			图形(面积 周长)计算器)
    			</h1>
    
    				<a href="index.php?action=rect">矩形</a><!-- action 是动作提交 -->
    				|| 
    
    				<a href="index.php?action=triangle">三角形</a>
    				||
    
    				<a href="index.php?action=circle">圆形</a>
    
    				<hr>
    	
    	</center>
    
            <?php
    								error_reporting ( E_ALL & ~ E_NOTICE );
    								//PHP遇到不认识的类就会调用该方法自己主动载入
    								function __autoload($className) 
    
    								{
    									include strtolower ( $className ) . ".class.php";
    								}
    								
    								/*
    								 *由于遇到Form类不认识,所以自己主动载入form.class.php 
    								 *   */
    								echo new Form ('index.php');
    								
    								if (isset ( $_POST ["sub"] )) {
    									
    									echo new Result ();
    								}
    								
    								?>
    
      </body>
    
    </html>
    

    做了这么几件事

    1.能够通过$GET_[“action”]方法和$_REQUEST ["action"]方法得到传入的參数是rect, triangle还是circle。

    2.通过echo new Form("index.php")和echo new Result()方法载入了form.class.php和result.class.php这两个类

    3.通过echo调用了这两个类的__toString方法


    接下来程序载入form.class.php这个文件

    <?php
    class Form {
    	private $action;
    	private $shape;
    	function __construct($action = "") {
    		$this->action = $action;
    		/* var_dump($this->action); */
    		$this->shape = isset ( $_REQUEST ["action"] ) ? $_REQUEST ["action"] : "rect";
    		/* var_dump($this->shape); */
    	}
    	
    
    	/* __toString() 方法用于一个类被当成字符串时应如何回应。比如 echo $obj; 应该显示些什么。此方法必须返回一个字符串 
    	 * 
    	 * 在此输出一个表单
    	 * 
    	 * */
    	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 .= '请选择一个形状';
    		}
    		$form .= '<input type="submit" name="sub" value="计算">';
    		
    		$form .= '</form>';
    		return $form;
    	}
    	private function getRect() {
    		$input = '<b>请输入|矩形|的长和宽:</b><p>';
    		$input .= '宽度:<input type="text" name="width" value="' . $_POST ['width'] . '"><br>';
    		$input .= '高度:<input type="test" name="height" value="' . $_POST ['height'] . '"><br>';
    		$input .= '<input type="hidden" name="action" value="rect">';
    		return $input;
    	}
    	private function getTriangle() {
    		$input = '<b>请输入|三角形|的三边:</b><p>';
    		$input = '<b>请输入|三角形|的三边:</b><p>';
    		$input .= '第一边:<input type="text" name="side1" value="' . $_POST ['side1'] . '"><br>';
    		$input .= '第二边:<input type="test" name="side2" value="' . $_POST ['side2'] . '"><br>';
    		$input .= '第三边:<input type="test" name="side3" value="' . $_POST ['side3'] . '"><br>';
    		$input .= '<input type="hidden" name="action" value="triangle">';
    		return $input;
    	}
    	private function getCircle() {
    		$input = '<b>请输入|圆形|的半径:</b><p>';
    		$input .= '半径:<input type="text" name="radius" value="' . $_POST ['radius'] . '"><br>';
    		$input .= '<input type="hidden" name="action" value="circle">';
    		return $input;
    	}
    }
    ?>

    这个php做了另外一件事,就是依据$_REQUEST ["action"]或者GET_["action"]载入了不同类型形状的表单。

    然后,一旦你按下计算button,接下来就转到了载入result.class.php这个文件

    <?php
     class Result{
                  private $shape;
                  /* 
                   * 依据form.class.php里传过来的$post['action']方法接受參数
                   *  */
                 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;//没有break会导致default的运行
                                 default:                        
                                       $this->shape=false;             
                        }
                }
      
                  function __toString(){          
                            if($this->shape){               
                                 $result=$this->shape->shapeName.'的周长'.$this->shape->perimeter().'<br>';
                                 $result.=$this->shape->shapeName.'的面积'.$this->shape->area().'<br>';
                                 return $result; }               
                             else{
                                 return'没有这个形状';       
                             }
    }
    }
      
     ?>
    

    这个文件做了一件事,就是分流,依据$_POST["action"]传过来的值看,运行那一个类文件

    下一篇讲各个类文件含义。

  • 相关阅读:
    Sql Server textcopy 的使用
    看来,我还是不适合犯罪
    Oracle 游标范例
    嵌入式学习方法 关于ARM+linux(转)
    存储控制器与外设之间的关系 (SDRAM与BANK6连接概述)
    socket和多线程可移植性编程基础(转)
    Linux设备驱动开发环境的搭建(转)
    Android JNI知识简介
    Linux USB驱动框架分析
    LINUX系统中动态链接库的创建和使用
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/3930140.html
Copyright © 2020-2023  润新知