<?php
header('Content-type:text/html;charset=utf-8');
//模拟统计在线学生人数
class Student{
//私有的静态的保存学生人数的属性
private static $count = 0;
//公共的构造方法
public function __construct(){
self::$count++;//人数增加方法
}
//获取在线人数
public function getCount(){
return self::$count;
}
//公共的析构方法
public function __destruct(){
self::$count--;//递减人数
echo "<br>当前在线学生人数为:".self::$count;
}
}
//创建学生类对象
$obj1 = new Student;
$obj2 = new Student;
$obj3 = new Student;
echo "当前共有".$obj2->getCount()."个学生在线!";
php模拟统计在线学生人数
最新推荐文章于 2021-06-01 21:16:32 发布