Created
February 18, 2015 12:05
-
-
Save saeidw/586347db2355a86862cb to your computer and use it in GitHub Desktop.
Type hint stupidity
This file contains 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 | |
// PROBLEM: I want to use type-hints to make sure the parameters of | |
// a function are all the right type. | |
function printColor(Color $theColor) { | |
echo "The color is " . $theColor->getValue() . PHP_EOL; | |
} | |
interface ValueType { | |
public static function getValue(); | |
} | |
interface Color extends ValueType {} | |
class Red implements Color { | |
public static function getValue() { | |
return 'red'; | |
} | |
} | |
class Blue implements Color { | |
public static function getValue() { | |
return 'blue'; | |
} | |
} | |
class Green implements Color { | |
public static function getValue() { | |
return 'green'; | |
} | |
} | |
// printColor('blue'); // FAILS | |
printColor(new Blue()); // WORKS! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment