PHP課程筆記10

來源:互聯網
上載者:User

標籤:

課時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
*/

 

PHP課程筆記10

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.