写一个针对php小白看的关于类与对象的小程序:
该程序由三个程序组合而成,主要是输出金字塔:
input.php:
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="print.php" method="post">
请输入金字塔的层数:<input type="text" name="layer">
<br>
<input type="submit" value="打印">
</form>
</body>
</html>
<?php
//接收MyInputView页面传来的数据
require_once "Print.class.php";
$layer = $_REQUEST["layer"];
//调用一个类来完成打印
$a = new abc();
$a->printStar($layer);
?>
Print.class.php
<?php
class abc{
//接收一个数,并打印金字塔
public function printStar($n){
//这里需要使用for循环了
for($i=1;$i<=$n;$i++){
for($k =1;$k<=$n-$i;$k++){
echo " ";
}
for($j=1;$j<=2*$i-1;$j++){
echo "*";
}
echo "<br>";
}
}
}
?>
希望能够帮助更好的理解一下PHP中的类与对象概念。