<?php
//以下代码用来展示静态修饰符所表达的含义
class Student{
//声明学生计算器,默认设置为0 ,用于统计学生的个数
private static $_Student_count=0;
//定义属性
private $_name;
//定义构造方法
public function __construct($_name){
$this->_name=$_name;
//每创建一个学生就自增
++ static::$_Student_count;
}
//克隆方法 自增
public function __clone(){
++ static::$_Student_count;
}
//销毁 就自减
public function __destruct(){
-- static::$_Student_count;
}
//获取学生的数量
public static function getStudentCount(){
return static::$_Student_count;
}
}
$s1=new Student("杨过");
echo '当前已经存在:';
echo Student::getStudentCount();
echo '名学生<br/>';
$s2=new Student("龙菇凉");
echo '当前已经存在:';
echo Student::getStudentCount();
echo '名学生<br/>';
$s3=new Student("李莫愁");
echo '当前已经存在:';
echo Student::getStudentCount();
echo '名学生<br/>';
unset($s3);
echo '当前已经存在:';
echo Student::getStudentCount();
echo '名学生<br/>';
?>
面向对象静态成员代码展示
最新推荐文章于 2022-05-06 20:48:14 发布