Skip to content

Instantly share code, notes, and snippets.

@saeidw
Created February 18, 2015 12:05
Show Gist options
  • Save saeidw/586347db2355a86862cb to your computer and use it in GitHub Desktop.
Save saeidw/586347db2355a86862cb to your computer and use it in GitHub Desktop.
Type hint stupidity
<?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