Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually I think it is not true: you can ovverride static propeties (<strong>you need >=5.3 PHP</strong> for that). But you have to be careful when refrencing for that static property (and this is the mistake in the original code)<br/> <br/> You need to use <strong>static::$myStaticProperty</strong> instead of using self::$myStaticProperty<br/> <br/> <strong>self::</strong> will refrence to the <strong>current class</strong> so if you are inside an inherited static method this will refrence the static property of that class defined that method! While using reference keyword static:: will act like $this - when you are using instance methods/propeties.<br/> <br/> doSomething() is an inherited static method in class Bar in your example. Since you used self:: there, it will reference to the static property of class Foo. This is the reason why you didn't see any difference... <strong>Try to change self:: to static::</strong>!<br/> <br/> Here is a code example - I used it myself to test those things. We have static property/method inheritance, override and value change in it - run it and you will see the result!<br/></p> <pre><code>class A { // a static property - we will test override with it protected static $var = 'class A var - override'; // a static property - we will test value overwrite with it protected static $var2 = 'class A var2 - value overwrite'; public static function myStaticOverridePropertyTest() { return static::$var; } public static function myStaticValueOverwritePropertyTest() { return static::$var2; } /** * This method is defined only here - class B will inherit this one! * We use it to test the difference btw self:: and static:: * * @return string */ public static function myStaticMethodTest() { //return self::getValue(); return static::getValue(); } /** * This method will be overwritten in class B * @return string */ protected static function getValue() { return 'value from class A'; } } class B extends A { // we override this inherited static property protected static $var = 'class B var - override'; /** * This method is overwritten from class A * @return string */ protected static function getValue() { return 'value from class B'; } /** * We modify the value of the inherited $var2 static property */ public static function modStaticProperty() { self::$var2 = 'class B - altered value! - value overwrite'; } } echo ("-- testing class A:\n"); echo (A::myStaticOverridePropertyTest(). "\n"); echo (A::myStaticValueOverwritePropertyTest(). "\n"); echo (A::myStaticMethodTest(). "\n"); echo ("-- now testing class B:\n"); echo (B::myStaticOverridePropertyTest(). "\n"); echo (B::myStaticValueOverwritePropertyTest(). "\n"); echo (" now invoking B::modStaticProperty() .\n"); B::modStaticProperty(); echo (B::myStaticValueOverwritePropertyTest(). "\n"); echo ("-- now re-testing class A:\n"); echo (A::myStaticOverridePropertyTest(). "\n"); echo (A::myStaticValueOverwritePropertyTest(). "\n"); echo (A::myStaticMethodTest(). "\n"); </code></pre> <p>This will output:<br/> <br/> -- testing class A:<br/> class A var - override<br/> class A var2 - value overwrite<br/> value from class A<br/> -- now testing class B:<br/> class B var - override<br/> class A var2 - value overwrite<br/> now invoking B::modStaticProperty() ...<br/> class B - altered value! - value overwrite<br/> -- now re-testing class A:<br/> class A var - override<br/> <strong>class B - altered value! - value overwrite</strong><br/> value from class A<br/> <br/> And here we are, you can see the difference between overriden and only value overwritten static properties... look the output line I marked with bold! When we invoked the modStaticProperty() of class B, it changed the value of that static variable in class A too. Since that static property was inherited and was not overriden! Think about it...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload