2011-09-04 43 views

回答

3

否(显然我在问题中看不到而不是)。 publicprotected静态方法和属性继承,你会想到他们是:

<?php 
class StackExchange { 
    public static $URL; 
    protected static $code; 
    private static $revenue; 

    public static function exchange() {} 

    protected static function stack() {} 

    private static function overflow() {} 
} 

class StackOverflow extends StackExchange { 
    public static function debug() { 
     //Inherited static methods... 
     self::exchange(); //Also works 
     self::stack(); //Works 
     self::overflow(); //But this won't 

     //Inherited static properties 
     echo self::$URL; //Works 
     echo self::$code; //Works 
     echo self::$revenue; //Fails 
    } 
} 

StackOverflow::debug(); 
?> 

静态属性和方法服从visibilityinheritance规则为this snippet所示。

17

不,这是不正确的。 Static Methods and properties将得到inherited一样的非静态方法和属性,并遵守同样的visibility rules:

class A { 
    static private $a = 1; 
    static protected $b = 2; 
    static public $c = 3; 
    public static function getA() 
    { 
     return self::$a; 
    } 
} 

class B extends A { 
    public static function getB() 
    { 
     return self::$b; 
    } 
} 

echo B::getA(); // 1 - called inherited method getA from class A 
echo B::getB(); // 2 - accessed inherited property $b from class A 
echo A::$c++; // 3 - incremented public property C in class A 
echo B::$c++; // 4 - because it was incremented previously in A 
echo A::$c;  // 5 - because it was incremented previously in B 

这些最后两个是显着的差异。增加基类中的继承静态属性也会在所有子类中增加它,反之亦然。