2011-09-04 7 views

답변

3

아니요 (분명히 질문에이 아닌 이 표시되지 않음). this snippet에 도시 된 바와 같이

<?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 규칙을 순종 : 당신이 그들을 것으로 기대하는 것처럼 publicprotected 정적 메서드와 속성이 상속됩니다.

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 

그 마지막 두가 주목할만한 차이가 있습니다 순종하는 것입니다. 기본 클래스에서 상속 된 정적 속성을 늘리면 모든 하위 클래스에서 증분됩니다 (반대의 경우도 마찬가지입니다).