PHP面向对象笔记 —— 132 延迟绑定复习

// ===代码部分1===

// =延迟绑定= //

class Animal {
    const age = 1;
    public static $leg = 4;

    public static function cry() {
        echo '呜呜
'
; } public static function t1() { self::cry(); echo self::age,'
'
; echo self::$leg,'
'
; } public static function t2() { static::cry(); echo static::age,'
'
; echo static::$leg,'
'
; } } class Human extends Animal { const age = 30; public static $leg = 2; public static function cry() { echo '哇哇
'
; } } /* class Stu extends Human { const age = 16; public static $leg = 3; public static function cry() { echo '嘤嘤
'; } } Stu::t1(); //呜呜,1,4 Stu::t2(); //嘤嘤,16,3 //static调用cry()方法会移植到自身Stu类来执行 */


// ===代码部分2===

class Stu extends Human {
    const age = 16;
    //public static $leg = 3;

    public static function cry() {
        echo '嘤嘤
'
; } } Stu::t2(); //嘤嘤,16,2 //把Stu中的leg拿掉,会转到父类中找leg,结果leg=2

你可能感兴趣的:(PHP,Object,Oriented,PHP面向对象笔记)