Created
September 27, 2013 19:11
-
-
Save darraghenright/6733690 to your computer and use it in GitHub Desktop.
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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? | |
* | |
* o_O | |
*/ | |
interface FooInterface | |
{ | |
/** | |
* Define a constant | |
*/ | |
const FOO = 'foo'; | |
} | |
class Bar implements FooInterface | |
{ | |
/** | |
* As noted in the PHP docs: | |
* | |
* An implementing class can | |
* not override an interface | |
* defined constant... | |
*/ | |
const FOO = 'bar'; // FATAL ERROR! | |
} | |
class Baz extends Bar | |
{ | |
/** | |
* However, a child of an | |
* implementing class can | |
* override an interface | |
* defined constant :) | |
*/ | |
const FOO = 'baz'; // WORKS! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment