1. Display data
Tables and classes correspond, table names are class names, and column names are members of a class
Each piece of data in the table corresponds to the object instantiated by the class
To create a class in Info.class.php:
<?php
Class Info
{
Public $code;
Public $name;
Public $sex;
Public $nation;
Public $birthday;
}
Load the info class on a new page:
<?php
Include ("Info.class.php");
$attr =array ();
$info 1=new Info ();
$info 1->code= "P001";
$info 1->name= "Zhang San";
$info 1->sex= "Male";
$info 1->nation= "Han";
$info 1->birthday= "1988-2-3";
Append an element to the array:
Array_push ($attr, $info 1);
$info 2= new Info ();
$info 2->code= "P002";
$info 2->name= "John Doe";
$info 2->sex= "female";
$info 2->nation= "Han";
$info 2->birthday= "1989-2-3";
Array_push ($attr, $info 2);
$info 3= new Info ();
$info 3->code= "P003";
$info 3->name= "Harry";
$info 3->sex= "Male";
$info 3->nation= "Hui";
$info 3->birthday= "1990-2-3";
Array_push ($attr, $info 3);
Var_dump ($ATTR);
echo "<table width= ' 100% ' border= ' 1 ' cellpadding= ' 0 ' cellspacing= ' 0 ' >";
echo "<tr><td> code </td><td> name </td><td> Gender </td><td> National </td> <td> birthday </td></tr> ";
foreach ($attr as $v)
{
echo "<tr>
<td>{$v->code}</td>
<td>{$v->name}</td>
<td>{$v->sex}</td>
<td>{$v->nation}</td>
<td>{$v->birthday}</td>
</tr> ";
}
echo "</table>";
?>
2. Find the difference of two circle area
2.1 Process-oriented:
$r 1=10;
$r 2=5;
$MJ = $r 1* $r 1*3.14-$r $r 2*3.14
Echo $MJ;
2.2 Object-oriented
Class Yuan
{
Public $r;
Function_construct ($r)
{
$this->r= $r;
}
function Mianji ()
{
return $this->r* $this->r*3.14
}
}
Load the Yuan class:
Include ("Yuan.class.php");
$y 1=new Yuan (10); Build a great circle
$y 2=new Yuan (5); Build a small Circle
echo $y 1->mianji ()-$y 2->mianji ();
Object-oriented practice