- 1静态成员2 class Stduent {
- 3 private static $a;
- 4
- }
- 5 6 Student: :$a; //需要加 $ ...对静态只有 获取或赋值
- 7::静态访问符范围解析符8 9在类内部用Studen: :$a;
- self: :$a;
- static: :$a;
- 10 11在继承中12静态成员也会被继承重写。13 14 self和static的区别。15 self: 永远绑定代码所在的类。当前定义类16 static:调用时,确定类。当前调用类作用:定义属性和方法,访问属性和方法。17 class goods {
- 18 public $title = 'goods';
- 19 public static
- function getTitleSelf() {
- 20 echo self: :$title;
- 21
- }
- 22 23 public static
- function getTitleStatic() {
- 24 echo static: :$title;
- 25
- }
- 26 27
- }
- 28 29 class Books {
- 30 public $title = 'books';
- 31
- }
- 32 33 echo Books: :getTitleSelf(); // echo goods;
- 34 echo Books: :getTitleStatic() // echo books;
- 35 36总结经常使用static符合逻辑。37 38 echo goods: :getTitleSelf(); // echo goods;
- 39 echo goods: :getTitleStatic(); // echo goods;
- 40 41总结:当类共享的数据和操作,定义为静态属性。42被类独有的数据和操作,定义为非静态属性。43 44 class Student {
- 45 pirvate static $s_count; //记录学生 增加 ++
- 46 47 public
- function __construct() {
- 48 echo++static: :$s_count;
- 49
- }
- 50 51 //当对象被销毁时候 ,应该减去
- 52 public
- function __destruct() {
- 53 echo--static: :$s_count;
- 54
- }
- 55 56 public
- function __clone() { //当对象被克隆的时候
- 57 echo++static: :$s_count;
- 58
- }
- 59
- }
- 60 61绑定在类上的关键字:self当前定义类parent父类static当前调用类62 63语法细节:64无论通过类或者对象都可以调用静态和非静态。局限于方法。属性不可以。65 66规范化:对象只调用非静态。类只调用静态。67 68 class Demo {
- 69 public
- function nonStaticFunc() {
- 70 echo "non static";
- 71
- }
- 72 public static
- function statucFunc() {
- 73 echo "static fun";
- 74
- }
- 75
- }
- 76 77 $d = new Demo();
- 78 $d - >nonstaticFunc(); //执行输出结果
- 79 $d - >staticFunc(); //执行输出结果 会报告一个 语法不严谨警告。
来源: http://www.bubuko.com/infodetail-1857049.html