137-PHP static修饰的类属性

<?php
    class test{        //定义一个类
        public static $num=0;        //定义static修饰的类属性
        public function __construct(){        //定义构造方法
            self::$num++;        //每次调用均使$num变量递增
        }
    }
    //实例化test类的三个对象
    $test1=new test;
    $test2=new test;
    $test3=new test;
    //输出test类实例化对象的个数
    echo 'test类一共实例化了'.test::$num.'个对象。';
?>

 

posted @ 2019-06-14 08:10  像一棵海草海草海草  阅读(127)  评论(0编辑  收藏  举报