1.类class
<?php
class guests{
//属性
private $name;
private $gender;
//方法
function setname($name){
$this->name = $name;
}
function getname(){
return $this->name;
}
function setgender($gender){
$this->gender = $gender;
}
function getgender(){
return $this->gender;
}
};
$lili = new guests;
$lili->setname('lim');
$lili->setgender('M');
$hu = new guests;
$hu->setname('hum');
$hu->setgender('F');
echo $lili->getname()."\t".$lili->getgender()."<br/>";
echo $hu->getname()."\t".$hu->getgender();
?>
2.构造函数
<?php
class guests{
private $name;
private $gender;
//__construct() 构造函数 无返回值
function __construct($name,$gender){
$this->name = $name;
$this->gender = $gender;
}
function getname(){
return $this->name;
}
function getgender(){
return $this->gender;
}
};
//当new实例时 构造函数被调用执行
$li = new guests("李明","男");
$fang = new guests("芳芳","女");
echo $li->getname()."\t".$li->getgender()."<br/>";
echo $fang->getname()."\t".$fang->getgender();
?>
3.访问函数
<?php
class guests{
public $property;
function __set($propName,$propValue){
$this->$propName = $propValue;
}
function __get($propName){
return $this->$propName;
}
};
$li = new guests;
$li->name = "李";
$li->gender = "男";
$yu = new guests;
$yu->name = "于";
$yu->gender = "女";
$yu->age = 20;
echo $li->name."是".$li->gender."<br/>";
echo $yu->name."是一位".$yu->age."岁".$yu->gender;
?>
4,类的继承与接口
<?php
class roomtypes{
public $customertype;
private $hotelname="GoodHome";
protected $roomface="适合所有人";
function __construct(){
$this->customertype="everyonefit";
}
function telltype(){
echo "此房间类型为".$this->customertype."。<br>";
}
function hotelface(){
echo "此房间".$this->roomface."。<br>";
}
final function welcomeshow(){
echo "欢迎光临".$this->hotelname."。<br>";
}
}
//nonvip
class nonviproom extends roomtypes{
function __construct(){
$this->customertype = "nonvip";
}
function telltype(){
echo "此".__CLASS__."房间类型为".$this->customertype."。<br>";
}
function hotelface(){
echo "此房间不是".$this->roomface."。<br>";
}
}
//vip
class viproom extends roomtypes implements showprice{
function __construct(){
$this->customertype="vip";
}
function showprice(){
if(__CLASS__=="viproom"){
echo "价格高于500元。<br>";
}
else{
echo "价格低于500元。<br>";
}
}
}
//supervip
final class superviproom implements showprice,showdetail{
function showprice(){
if(__CLASS__=="superviproom"){
echo "价格高于500元。<br>";
}
else{
echo "价格低于500元。<br>";
}
}
function showdetail(){
if(__CLASS__=="superviproom"){
echo "超级VIP客户可以使用会员卡取得优惠。<br>";
}
else{
echo "普通用户与VIP用户不能使用会员卡。<br>";
}
}
}
//声明接口
interface showprice{
function showprice();
}
interface showdetail{
function showdetail();
}
$room2046 = new roomtypes();
$room2046->telltype();
$room2046->hotelface();
$room2046->welcomeshow();
$room307 = new nonviproom();
$room307->telltype();
$room307->hotelface();
$room2 = new viproom();
$room2->telltype();
$room2->showprice();
$roomsuper3 = new superviproom();
$roomsuper3->showprice();
$roomsuper3->showdetail();
?>
4,异常处理
<?php
try{
if(!isset($i)){ //判断变量是否被定义
throw new Exception('$i未定义'); //报告一个异常
}
echo "程序运行结束";
}
catch(Exception $e){ //捕获并处理异常
echo "程序未正常执行完毕!"."错误信息为:".$e->getMessage();
}
?>
5.会话管理
<html>
<head>
<title>
</title>
</head>
<body>
<?php
session_start();
echo "会话变量为:".$_SESSION['name'];
echo $_SESSION['name']."你好";
?>
<a href="8.php">下一页</a>
</body>
</html>
<html>
<head>
<title>
</title>
</head>
<body>
<?php
session_start();
unset($_SESSION['name']);
if(isset($_SESSION['name'])){
echo "会话变量为:".$_SESSION['name'];
}
else{
echo "会话变量已注销。";
}
?>
</body>
</html>
<html>
<head>
<title>
</title>
</head>
<body>
<?php
/*setcookie('mycookie','true',time()+60*60) or die(); //设置Cookie
echo 'Coolie设置成功'; //输出提示信息*/
session_start();
$_SESSION['name']="王小明";
echo "会话变量为:".$_SESSION['name'];
?>
<a href="7.php">下一页</a>
</body>
</html>