Re: Protected static props redeclared as public share value across classes

From: Date: Mon, 28 Jan 2008 16:49:26 +0000
Subject: Re: Protected static props redeclared as public share value across classes
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-34965@lists.php.net to get a copy of this message
Hello Robin, I checked it out in more detail and it is indeed broken as in it is not consistent: [marcus@zaphod PHP_5_3]$ php -r 'class A { protected static $p=1; } class B extends A { protected static $p=2; } ReflectionClass::Export("B");' -> works == 2 properties -> but should fail because of changed value [marcus@zaphod PHP_5_3]$ php -r 'class A { protected static $p=1; } class B extends A { public static $p=2; } ReflectionClass::Export("B");' Fatal error: Cannot change initial value of property static protected A::$p in class B in Command line code on line 1 [marcus@zaphod PHP_5_3]$ php -r 'class A { public static $p=1; } class B extends A { protected static $p=2; } ReflectionClass::Export("B");' Fatal error: Access level to B::$p must be public (as in class A) in Command line code on line 1 [marcus@zaphod PHP_5_3]$ php -r 'class A { public static $p=1; } class B extends A { public static $p=2; } ReflectionClass::Export("B");' -> works == 2 properties -> but should fail becasue of changed value So we need to fix this. marcus Monday, January 28, 2008, 4:18:50 PM, you wrote: > Hi Marcus, > Thanks for the prompt reply and explanation. I have some further > questions though: >> If the base class had the property defined as private >> then the property is private to that specific class and not directly >> accessible by derived classes that is it's name gets prefixed with the class >> name.. So in: >> class A { private static $p; } class B extends A { private static $p; } >> we have two different properties: > Understood. But if we have two separate properties for the reason that > A::$p is not visible to B, then how about these cases? > class A { protected static $p; } class B extends A { protected static $p; } > class A { public static $p; } class B extends A { public static $p; } > In both of those cases, A::$p is visible to the derived class, but the > re-declaration results in A::$p and B::$p being two separate > properties (see pastebin.com/fca2cd5b and pastebin.com/f4f94b32d for a > demonstration). This is one of the reasons I find the case where we > end up with only one property value to be surprising. > Another reason is that, as illustrated in my previous post, PHP's > behaviour doesn't seem to correlate with the inheritance rules of > other languages I'm familiar with: you always end up with two distinct > static properties in Java, C++ and C# (though of course I understand > this fact on its own is does not mean PHP is wrong :). > Lastly, with overridden static methods, PHP always yields two distinct > methods, regardless of the visibility modifiers. See > http://pastebin.com/f27f009c4 . Granted, with methods any other > behaviour would be very odd indeed, but it does emphasize an > inconsistency between method and property inheritance rules in PHP. > So for now I continue to feel this is a little strange. Any further > explanations would be greatly appreciated. :) > Thanks, > Robin >> Best regards, >> Marcus Best regards, Marcus

« previous php.internals (#34965) next »