2011-09-04 22 views

risposta

3

No (A quanto pare non ho potuto vedere il non nella domanda). public e protected metodi e proprietà statiche vengono ereditate come ci si aspetta che siano:

<?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(); 
?> 

proprietà e metodi statici obbedire alle regole visibility e inheritance come illustrato nella this snippet.

17

No. Non è vero. Static Methods and properties otterrà inherited uguale a metodi e proprietà non statici e obbedire stesso 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 

Questi ultimi due sono la notevole differenza. L'incremento di una proprietà statica ereditata nella classe base verrà incrementato anche in tutte le classi figlie e viceversa.