Posted By

denright on 01/04/11


Tagged

php interface


Versions (?)

Interface-defined constants and implementing classes.


 / Published in: PHP
 

A class that implements an interface cannot override a constant defined in the interface. But any child class that extends from the implementing class can.

  1. <?php
  2.  
  3. interface FooInterface
  4. {
  5. /**
  6.   * Define a constant
  7.   */
  8. const FOO = 'foo';
  9. }
  10.  
  11. class Bar implements FooInterface
  12. {
  13. /**
  14.   * As noted in the PHP docs:
  15.   *
  16.   * An implementing class can
  17.   * not override an interface
  18.   * defined constant...
  19.   */
  20. const FOO = 'bar'; // FATAL ERROR!
  21. }
  22.  
  23. class Baz extends Bar
  24. {
  25. /**
  26.   * However, a child of an
  27.   * implementing class can
  28.   * override an interface
  29.   * defined constant :)
  30.   */
  31. const FOO = 'baz'; // WORKS!
  32. }

Report this snippet  

You need to login to post a comment.