php类与对象综合案例

<?php
//类常量的定义与访问
class Stu
{
    private $name;
    private $sex;
    private $age;

    private static $count = 0;
    const COUNTY = '我们共同的家园---中国';

    public function __construct($name2,$sex2,$age2)
    {
        $this->name = $name2;
        $this->sex = $sex2;
        $this->age = $age2;
        self::$count++;//总数加加
       $this->info();
    }
    public function info()
    {
        $str = '<h2>'.self::COUNTY.'</h2>';
        $str .= "我的姓名是".$this->name.'<br>';
        $str .= "我的性别是".$this->sex.'<br>';
        $str .= "我的年龄是".$this->age.'<br>';
        $str .= $this->name.'加入中国,总共有'.self::$count.'个人';
        echo $str;
    }
    public function __destruct()
    {
        self::$count--;//总数减减
    }
}

$obj = new Stu('明哥哥','男',26);
$obj2 = new Stu('明哥哥2','男',26);
unset($obj2);
$obj3 = new Stu('明哥哥3','男',26); $obj4 = new Stu('明哥哥4','男',26);

原文地址:https://www.cnblogs.com/xm666/p/11255391.html