php面向对象之-延迟绑定复习-132

来源:互联网 发布:农产品b2c网站数据库 编辑:程序博客网 时间:2024/06/05 17:30

<span style="font-size:14px;"><?php //01.php /**** 燕十八 公益PHP讲堂 论  坛: http://www.zixue.it 微  博: http://weibo.com/Yshiba YY频道: 88354001 ****/ /*** ====笔记部分==== ***/ class Animal {     const age = 1;     public static $leg = 4;     public static function cry() {         echo '呜呜<br />';     }     public static function t1() {         self::cry();         echo self::age,'<br />';         echo self::$leg,'<br />';     }     public static function t2() {         static::cry();         echo static::age,'<br />';         echo static::$leg,'<br />';     } } class Human extends Animal {     public static $leg = 2;     public static function cry() {         echo '哇哇<br />';     } } class Stu extends Human {     const age = 16;     public static function cry() {         echo '嘤嘤<br />';     } } Stu::t1(); //呜呜,1,4 Stu::t2(); // 嘤嘤,16,2 ?></span><span style="font-size: 18px;"></span>



对于Stu:t1()的结果:先在Stu类中找t1, 找不到,到Human类中找,也没有,继续找Animal,有t1, 因此绑定了Animal                                     类,所以结果应该是self绑定为Animal,因此应该 是 echo $Animal::age;   echo $Animal::leg;  


对于Stu:t2()的结果:先在Stu类中找t2, 找不到,到Human类中找,也没有,继续找Animal,有t2,而此前加的是                                           static,先绑定Animal,再绑定Stu类,所以结果应该是self绑定为Animal,因此应该 是 echo                                           $Animal::leg;   echo $Stu::age 。


(不要问为什么会这样,我也不知道,因为php中的运行机制就是这样)


0 0