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 revisionBoth sides next revision
rfc:protectedlookup [2008/06/01 11:15] robinfrfc:protectedlookup [2008/06/03 13:35] robinf
Line 251: Line 251:
   * 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().
  
 +
 +===== Appendix =====
 +==== Other potential LSP violations ====
 +If Option 1 is rejected on ground of breach of LSP, then other arguable violations of LSP should be reviewed too. 
 +Below is a list of examples to be considered.
 +=== Private static methods ===
 +<code php>
 +<?php
 +class P {
 +   private function f() { echo "In " . __METHOD__ . "\n"; }
 +   private static function sf() { echo "In " . __METHOD__ . "\n"; }
 +   
 +   static function test(P $liskov) {
 +      $class = get_class($liskov);
 +      echo "Instance method call on instance of $class: ";
 +      $liskov->f(); // if $liskov instanceof C, falls back to P::f()
 +      echo "Static method call on $class: ";
 +      $class::sf(); // if $liskov instanceof C, does not fall back to P::f() - fatal error.
 +   }
 +}
 +
 +class C extends P {
 +   private function f() { echo "In " . __METHOD__ . "\n"; }
 +   private static function sf() { echo "In " . __METHOD__ . "\n"; }
 +}
 +
 +P::test(new P);
 +P::test(new C); // Valid Liskov substitution - should this fail?
 +?>
 +</code>
 +=== Private static properties ===
 +<code php>
 +<?php
 +class P {
 +   private $a = 'P::$a';
 +   private static $sa = 'P::$sa';
 +   
 +   static function test(P $liskov) {
 +      $class = get_class($liskov);
 +      echo "Instance property access on instance of $class: ";
 +      echo $liskov->a . "\n"; // if $liskov instanceof C, falls back to P::$a
 +      echo "Static property access call on $class: ";
 +      echo C::$sa . "\n"; // if $liskov instanceof C, does not fall back to P::$sa - fatal error.
 +   }
 +}
 +
 +class C extends P {
 +   private $a = 'C::$a';
 +   private static $sa = 'C::$sa';
 +}
 +
 +P::test(new P);
 +P::test(new C); // Valid Liskov substitution - should this fail?
 +?>
 +</code>
rfc/protectedlookup.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1