rfc:protectedlookup

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:protectedlookup [2008/06/03 13:36] robinfrfc:protectedlookup [2008/06/03 13:52] robinf
Line 218: Line 218:
   * The protected modifier loses its intuitive/documented meaning, since protected members may be accessible from siblings.   * The protected modifier loses its intuitive/documented meaning, since protected members may be accessible from siblings.
      
 +
 ==== Option 3 ==== ==== Option 3 ====
 This approach is similar to option 2, but modifies the new rule slightly so as to preserve the intuitive meaning of the protected modifier. Lookups of protected members on sibling classes fall back to the declaration from the common ancestor class, if available. To illustrate: This approach is similar to option 2, but modifies the new rule slightly so as to preserve the intuitive meaning of the protected modifier. Lookups of protected members on sibling classes fall back to the declaration from the common ancestor class, if available. To illustrate:
Line 249: Line 250:
 === Cons === === Cons ===
   * Non-trivial code change   * Non-trivial code change
-  * Possibly confusing at first, as code that reads C1::f() may in fact result in an invocation of P::f().+  * Possibly confusing at first, as code that reads C1::f() may in fact result in an invocation of P::f(). However, this behaviour would be comparable to existing behaviour when accessing re-declared private members of child classes. For example: 
 +<code php> 
 +<?php 
 +// Class P declares some private members. 
 +class P { 
 +  private function f() { echo 'P::f()';
 +  public static function test() { 
 +    $c = new C; 
 +    $c->f();  // falls back to P::f() prints P::f() 
 +  } 
 +
 +  
 +// Class C1 re-declares the "inherited" private members. 
 +class C extends P { 
 +  private function f() { echo 'C::f()';
 +
 + 
 +P::test(); 
 +?> 
 +</code> 
 + 
  
  
Line 293: Line 315:
       echo $liskov->a . "\n"; // if $liskov instanceof C, falls back to P::$a       echo $liskov->a . "\n"; // if $liskov instanceof C, falls back to P::$a
       echo "Static property access call on $class: ";       echo "Static property access call on $class: ";
-      echo C::$sa . "\n"; // if $liskov instanceof C, does not fall back to P::$sa - fatal error.+      echo $class::$sa . "\n"; // if $liskov instanceof C, does not fall back to P::$sa - fatal error.
    }    }
 } }
rfc/protectedlookup.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1