PHP课程笔记10

来源:互联网 发布:传世db数据库编辑器 编辑:程序博客网 时间:2024/04/29 12:38

课时176 PHP图形计算器需求分析

<?php
/*
* 一个形状的抽象类,定义子类必须实现的方法
*
*
*
*/
abstract class shape {
public $name; //形状的名字

abstract function area(); //求面积

abstract function zhouchang(); //求周长

abstract function view(); //形状的图形表单

abstract function check(); //验证是否是该形状
}
?>


课时177 PHP图形计算器功能设计

<?php
/*
* 这个类是一个矩形的类,这个类要按形状的规范去实现
*
*/
class rect extends shape {
private $width;
private $height;

function __construct($arr){
$this->width = $arr['width'];
$this->height = $arr['height'];
$this->name = $arr['name'];
}

function area(){
return $this->width * $this->height;
}
function zhouchang() {
return 2*($this->width + $this0->height);
}
function view(){
$form = '<form action ="index.php" method="post">';
$form .= $this->name.'的宽:<input type="text" name="width" value="" /><br/>';
$form .= $this->name.'的高:<input type="text" name="height" value="" /><br/>';
$form .= '<input type = "submit" name="dosubmit" value="计算"><br>';
$form .= "<form>";
}
function check(){
$bg = true;
if($arr['width']<0){
echo $this->name."的宽不能小于0<br>";
$bg = false;
}
if($arr['height']<0){
echo $this->name."的高不能小于0<br>";
$bg = false;
}

return $bg;

}
}
?>


课时178 PHP图形计算器主程序的实现

<?php
/*
* 在<a>连接里用一个参数区分用户的操作,作为测试使用
*
*/
?>

<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> ||
<a href="index.php>action=triangle">三角形</a>
</center>

<hr/>

<?php
//判断用户是否有选择单机一个形状链接
if(!empty($_GET['action'])){

echo $_GET['action'];

//如果用户没有单机连接,则是默认访问这个主程序
}else{
echo "请选择一个要计算的图形!<br>";
}

?>
</body>
</html>


课时179 PHP图形计算器主程序的步骤及代码

<?php
/*
* 在<a>连接里用一个参数区分用户的操作,作为测试使用
*
*/
error_reporting(E_ALL ^ E_NOTICE);
?>

<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> ||
<a href="index.php?action=triangle">三角形</a>
</center>

<hr/>

<?php

//设置自动加载这个程序需要的类文件
function __autoload($classname){
include strtolower($classname).".class.php";
}

//判断用户是否有选择单机一个形状链接
if(!empty($_GET['action'])){
//1.创建形状的对象
$classname = $_GET['action'];

$shape = new $classname($_POST);
//2.调用图形的对象中的界面view()

$shape -> view();

//3.用户是否提交了对应图形界面的表单
if(isset($_POSP['dosubmit'])){

}

//4.查看用户输入的数据是否正确,失败则提示
if($shape->check($_POST)){
//5.计算图形周长和面积
echo $shape->name."周长为:".$shape -> zhouchang()."<br>";
echo $shape->name."面积为:".$shape -> area()."<br>";
}
//如果用户没有单机连接,则是默认访问这个主程序
}else{
echo "请选择一个要计算的图形!<br>";
}

?>
</body>
</html>

结果:

 

简单的图形计算器

矩形 || 三角形

 


请选择一个要计算的图形!


课时180 PHP图形计算器主程序完成检验

<?php
/*
* 在<a>连接里用一个参数区分用户的操作,作为测试使用
*
*/
error_reporting(E_ALL ^ E_NOTICE);
?>

<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> ||
<a href="index.php?action=triangle">三角形</a> ||
<a href="index.php?action=circle">圆形</a>
</center>

<hr/>

<?php

//设置自动加载这个程序需要的类文件
function __autoload($classname){
include strtolower($classname).".class.php";
}

//判断用户是否有选择单机一个形状链接
if(!empty($_GET['action'])){
//1.创建形状的对象
$classname = $_GET['action'];

$shape = new $classname($_POST);
//2.调用图形的对象中的界面view()

$shape -> view();

//3.用户是否提交了对应图形界面的表单
if(isset($_POSP['dosubmit'])){

}

//4.查看用户输入的数据是否正确,失败则提示
if($shape->check($_POST)){
//5.计算图形周长和面积
echo $shape->name."周长为:".$shape -> zhouchang()."<br>";
echo $shape->name."面积为:".$shape -> area()."<br>";
}
//如果用户没有单机连接,则是默认访问这个主程序
}else{
echo "请选择一个要计算的图形!<br>";
}

?>
</body>
</html>

 

circle.class.php

 

<?php

class circle extends shape {
private $r;
const PI = 3.14;

function __construct($arr = array()) {
if(!empty($arr)){
$this->r = $arr['r'];
}
$this ->name = "圆形";
}

function area(){
return self::PI * $this->r * $this->r;
}

function zhouchang(){
return 2 * self::PI * $this ->r;
}

function view(){
$form = '<form action ="index.php?action=circle" method="post">';
$form .= $this->name.'的半径:<input type="text" name="r" value="'.$_POST['r'].'" /><br/>';
$form .= '<input type = "submit" name="dosubmit" value="计算"><br>';
$form .= "<form>";
echo $form;
}

function check($arr){
$bg = true;
if($arr['r'] <= 0){
echo $this->name."的半径不能小于0<br>";
$bg = false;
}
return $bg;
}
}

?>

 

triangle.class.php

 

<?php
/*
* 这是按形状的抽象类写的一个三角形的计算方式
*
*
*/

class triangle extends shape {
private $bian1;
private $bian2;
private $bian3;

function __construct($arr=array()){
if(!empty($arr)){
$this->bian1 = $arr['bian1'];
$this->bian2 = $arr['bian2'];
$this->bian3 = $arr['bian3'];

}
$this->name = "三角形";

}
function area(){
$p = ($this->bian1 + $this ->bian2 + $this->bian3)/2;
return sqrt($p*($p-$this->bian1)*($p-$this->bian2)*($p-$this->bian3));

}

function view(){
$form = '<form action ="index.php?action=triangle" method="post">';
$form .= $this->name.'的边1:<input type="text" name="bian1" value="'.$_POST['bian1'].'" /><br/>';
$form .= $this->name.'的边2:<input type="text" name="bian2" value="'.$_POST['bian2'].'" /><br/>';
$form .= $this->name.'的边3:<input type="text" name="bian3" value="'.$_POST['bian3'].'" /><br/>';
$form .= '<input type = "submit" name="dosubmit" value="计算"><br>';
$form .= "<form>";
echo $form;
}

function zhouchang() {
return $this->bian1 + $this->bian2 + $this->bian3;

}
function check($arr){
$bj = true;
if($arr['bian1'] < 0){
echo "第一个边不能小于0";
$bj = false;
}
if($arr['bian2'] < 0){
echo "第二个边不能小于0";
$bj = false;
}
if($arr['bian3'] < 0){
echo "第三个边不能小于0";
$bj = false;
}

if($arr['bian1']+$arr['bian2']<$arr['bian3'] || $arr['bian1']+$arr['bian3']<$arr['bian2'] || $arr['bian3']+$arr['bian2']<$arr['bian1']){
echo "两边只和必须大于第三边";
$bj = false;
}

return $bj;
}
}
?>

 


课时181 与类有关的魔术常量和函数

 

 

<?php
class Demo{
function test(){
echo __CLASS__."<br>";
$arr = array();
array_filter($arr,array(__CLASS__,"hello"));

echo __METHOD__;
}

function hello(){

}

}

$d = new Demo;
$d ->test();
?>

结果:

Demo
Demo::test


课时182 PHP命名空间的概述

/*
* 命名空间
*
* 1.常量
* 2.函数
* 3.类
*
* namespace
*/


 

0 0