为什么会有静态属性
只是为了方便不用实例化一个类, 即可使用里面的方法
代码练习
- class math{static public function add($a,$b){
- return $a+$b;
- }
- }
- $sum = new math();
- $sum -> add(2,3);
这样写的返回结果是 5
但是也可以不用去实例化这个类需要在方法前加 static
使用技巧 math::add(2,3) 即可 就不用实例化这个类
类常量的调用
- define('PI',3.1415926);
- class math{
- public function addc(){
- echo PI; // 调用的是全局常量
- }
- }
- $bb = new math();
- $bb->addc();
那么问题来了 如果我需要调用类里面的常量则需要这么写
- class math{
- const PI = 3.1415926;
- public function addc(){
- echo math::PI;
- }
- }
- $bb = new math();
- $bb -> addc();
来源: http://www.bubuko.com/infodetail-2573039.html